1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
45 #include <QActionGroup>
47 #include <QPlainTextEdit>
49 #include <QVBoxLayout>
51 # include <QScrollArea>
52 # include <QVBoxLayout>
53 # include "texteditautoresizer_maemo5.h"
56 #include "loggerwidget.h"
60 LoggerWidget::LoggerWidget(QWidget *parent) :
62 m_visibilityOrigin(SettingsOrigin)
64 setAttribute(Qt::WA_QuitOnClose, false);
65 setWindowTitle(tr("Warnings"));
67 m_plainTextEdit = new QPlainTextEdit();
69 #if defined(Q_OS_SYMBIAN)
70 QAction* clearAction = new QAction(tr("Clear"), this);
71 clearAction->setSoftKeyRole(QAction::PositiveSoftKey);
72 connect(clearAction, SIGNAL(triggered()), m_plainTextEdit, SLOT(clear()));
73 addAction(clearAction);
75 m_plainTextEdit->setReadOnly(true);
76 QAction* backAction = new QAction( tr("Back"), this );
77 backAction->setSoftKeyRole( QAction::NegativeSoftKey );
78 connect(backAction, SIGNAL(triggered()), this, SLOT(hide()));
79 addAction( backAction );
83 new TextEditAutoResizer(m_plainTextEdit);
84 setAttribute(Qt::WA_Maemo5StackedWindow);
85 QScrollArea *area = new QScrollArea();
86 area->setWidget(m_plainTextEdit);
87 area->setWidgetResizable(true);
88 setCentralWidget(area);
90 setCentralWidget(m_plainTextEdit);
93 m_noWarningsLabel = new QLabel(m_plainTextEdit);
94 m_noWarningsLabel->setText(tr("(No warnings)"));
95 m_noWarningsLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
96 QVBoxLayout *layout = new QVBoxLayout;
97 layout->addWidget(m_noWarningsLabel);
98 m_plainTextEdit->setLayout(layout);
99 connect(m_plainTextEdit, SIGNAL(textChanged()), this, SLOT(updateNoWarningsLabel()));
102 setupPreferencesMenu();
105 void LoggerWidget::append(const QString &msg)
107 m_plainTextEdit->appendPlainText(msg);
109 if (!isVisible() && (defaultVisibility() == AutoShowWarnings))
113 LoggerWidget::Visibility LoggerWidget::defaultVisibility() const
118 void LoggerWidget::setDefaultVisibility(LoggerWidget::Visibility visibility)
120 if (m_visibility == visibility)
123 m_visibility = visibility;
124 m_visibilityOrigin = CommandLineOrigin;
126 m_preferencesMenu->setEnabled(m_visibilityOrigin == SettingsOrigin);
129 QMenu *LoggerWidget::preferencesMenu()
131 return m_preferencesMenu;
134 QAction *LoggerWidget::showAction()
136 return m_showWidgetAction;
139 void LoggerWidget::readSettings()
142 QString warningsPreferences = settings.value(QLatin1String("warnings"), QLatin1String("hide")).toString();
143 if (warningsPreferences == QLatin1String("show")) {
144 m_visibility = ShowWarnings;
145 } else if (warningsPreferences == QLatin1String("hide")) {
146 m_visibility = HideWarnings;
148 m_visibility = AutoShowWarnings;
152 void LoggerWidget::saveSettings()
154 if (m_visibilityOrigin != SettingsOrigin)
157 QString value = QLatin1String("autoShow");
158 if (defaultVisibility() == ShowWarnings) {
159 value = QLatin1String("show");
160 } else if (defaultVisibility() == HideWarnings) {
161 value = QLatin1String("hide");
165 settings.setValue(QLatin1String("warnings"), value);
168 void LoggerWidget::warningsPreferenceChanged(QAction *action)
170 Visibility newSetting = static_cast<Visibility>(action->data().toInt());
171 m_visibility = newSetting;
175 void LoggerWidget::showEvent(QShowEvent *event)
177 QWidget::showEvent(event);
181 void LoggerWidget::closeEvent(QCloseEvent *event)
183 QWidget::closeEvent(event);
187 void LoggerWidget::setupPreferencesMenu()
189 m_preferencesMenu = new QMenu(tr("Warnings"));
190 QActionGroup *warnings = new QActionGroup(m_preferencesMenu);
191 warnings->setExclusive(true);
193 connect(warnings, SIGNAL(triggered(QAction*)), this, SLOT(warningsPreferenceChanged(QAction*)));
195 QAction *showWarningsPreference = new QAction(tr("Show by default"), m_preferencesMenu);
196 showWarningsPreference->setCheckable(true);
197 showWarningsPreference->setData(LoggerWidget::ShowWarnings);
198 warnings->addAction(showWarningsPreference);
199 m_preferencesMenu->addAction(showWarningsPreference);
201 QAction *hideWarningsPreference = new QAction(tr("Hide by default"), m_preferencesMenu);
202 hideWarningsPreference->setCheckable(true);
203 hideWarningsPreference->setData(LoggerWidget::HideWarnings);
204 warnings->addAction(hideWarningsPreference);
205 m_preferencesMenu->addAction(hideWarningsPreference);
207 QAction *autoWarningsPreference = new QAction(tr("Show for first warning"), m_preferencesMenu);
208 autoWarningsPreference->setCheckable(true);
209 autoWarningsPreference->setData(LoggerWidget::AutoShowWarnings);
210 warnings->addAction(autoWarningsPreference);
211 m_preferencesMenu->addAction(autoWarningsPreference);
213 switch (defaultVisibility()) {
214 case LoggerWidget::ShowWarnings:
215 showWarningsPreference->setChecked(true);
217 case LoggerWidget::HideWarnings:
218 hideWarningsPreference->setChecked(true);
221 autoWarningsPreference->setChecked(true);
225 void LoggerWidget::updateNoWarningsLabel()
227 m_noWarningsLabel->setVisible(m_plainTextEdit->toPlainText().length() == 0);