1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the tools applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QActionGroup>
47 #include <QPlainTextEdit>
49 #include <QVBoxLayout>
51 #include "loggerwidget.h"
55 LoggerWidget::LoggerWidget(QWidget *parent) :
57 m_visibilityOrigin(SettingsOrigin)
59 setAttribute(Qt::WA_QuitOnClose, false);
60 setWindowTitle(tr("Warnings"));
62 m_plainTextEdit = new QPlainTextEdit();
63 setCentralWidget(m_plainTextEdit);
64 m_noWarningsLabel = new QLabel(m_plainTextEdit);
65 m_noWarningsLabel->setText(tr("(No warnings)"));
66 m_noWarningsLabel->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
67 QVBoxLayout *layout = new QVBoxLayout;
68 layout->addWidget(m_noWarningsLabel);
69 m_plainTextEdit->setLayout(layout);
70 connect(m_plainTextEdit, SIGNAL(textChanged()), this, SLOT(updateNoWarningsLabel()));
73 setupPreferencesMenu();
76 void LoggerWidget::append(const QString &msg)
78 m_plainTextEdit->appendPlainText(msg);
80 if (!isVisible() && (defaultVisibility() == AutoShowWarnings))
84 LoggerWidget::Visibility LoggerWidget::defaultVisibility() const
89 void LoggerWidget::setDefaultVisibility(LoggerWidget::Visibility visibility)
91 if (m_visibility == visibility)
94 m_visibility = visibility;
95 m_visibilityOrigin = CommandLineOrigin;
97 m_preferencesMenu->setEnabled(m_visibilityOrigin == SettingsOrigin);
100 QMenu *LoggerWidget::preferencesMenu()
102 return m_preferencesMenu;
105 QAction *LoggerWidget::showAction()
107 return m_showWidgetAction;
110 void LoggerWidget::readSettings()
113 QString warningsPreferences = settings.value(QLatin1String("warnings"), QLatin1String("hide")).toString();
114 if (warningsPreferences == QLatin1String("show")) {
115 m_visibility = ShowWarnings;
116 } else if (warningsPreferences == QLatin1String("hide")) {
117 m_visibility = HideWarnings;
119 m_visibility = AutoShowWarnings;
123 void LoggerWidget::saveSettings()
125 if (m_visibilityOrigin != SettingsOrigin)
128 QString value = QLatin1String("autoShow");
129 if (defaultVisibility() == ShowWarnings) {
130 value = QLatin1String("show");
131 } else if (defaultVisibility() == HideWarnings) {
132 value = QLatin1String("hide");
136 settings.setValue(QLatin1String("warnings"), value);
139 void LoggerWidget::warningsPreferenceChanged(QAction *action)
141 Visibility newSetting = static_cast<Visibility>(action->data().toInt());
142 m_visibility = newSetting;
146 void LoggerWidget::showEvent(QShowEvent *event)
148 QWidget::showEvent(event);
152 void LoggerWidget::closeEvent(QCloseEvent *event)
154 QWidget::closeEvent(event);
158 void LoggerWidget::setupPreferencesMenu()
160 m_preferencesMenu = new QMenu(tr("Warnings"));
161 QActionGroup *warnings = new QActionGroup(m_preferencesMenu);
162 warnings->setExclusive(true);
164 connect(warnings, SIGNAL(triggered(QAction*)), this, SLOT(warningsPreferenceChanged(QAction*)));
166 QAction *showWarningsPreference = new QAction(tr("Show by default"), m_preferencesMenu);
167 showWarningsPreference->setCheckable(true);
168 showWarningsPreference->setData(LoggerWidget::ShowWarnings);
169 warnings->addAction(showWarningsPreference);
170 m_preferencesMenu->addAction(showWarningsPreference);
172 QAction *hideWarningsPreference = new QAction(tr("Hide by default"), m_preferencesMenu);
173 hideWarningsPreference->setCheckable(true);
174 hideWarningsPreference->setData(LoggerWidget::HideWarnings);
175 warnings->addAction(hideWarningsPreference);
176 m_preferencesMenu->addAction(hideWarningsPreference);
178 QAction *autoWarningsPreference = new QAction(tr("Show for first warning"), m_preferencesMenu);
179 autoWarningsPreference->setCheckable(true);
180 autoWarningsPreference->setData(LoggerWidget::AutoShowWarnings);
181 warnings->addAction(autoWarningsPreference);
182 m_preferencesMenu->addAction(autoWarningsPreference);
184 switch (defaultVisibility()) {
185 case LoggerWidget::ShowWarnings:
186 showWarningsPreference->setChecked(true);
188 case LoggerWidget::HideWarnings:
189 hideWarningsPreference->setChecked(true);
192 autoWarningsPreference->setChecked(true);
196 void LoggerWidget::updateNoWarningsLabel()
198 m_noWarningsLabel->setVisible(m_plainTextEdit->toPlainText().length() == 0);