Remove Q_WS_*, symbian and maemo code in QtDeclarative
[profile/ivi/qtdeclarative.git] / tools / qmlviewer / loggerwidget.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qglobal.h>
43 #include <QDebug>
44 #include <QSettings>
45 #include <QActionGroup>
46 #include <QMenu>
47 #include <QPlainTextEdit>
48 #include <QLabel>
49 #include <QVBoxLayout>
50
51 #include "loggerwidget.h"
52
53 QT_BEGIN_NAMESPACE
54
55 LoggerWidget::LoggerWidget(QWidget *parent) :
56     QMainWindow(parent),
57     m_visibilityOrigin(SettingsOrigin)
58 {
59     setAttribute(Qt::WA_QuitOnClose, false);
60     setWindowTitle(tr("Warnings"));
61
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()));
71
72     readSettings();
73     setupPreferencesMenu();
74 }
75
76 void LoggerWidget::append(const QString &msg)
77 {
78     m_plainTextEdit->appendPlainText(msg);
79
80     if (!isVisible() && (defaultVisibility() == AutoShowWarnings))
81         setVisible(true);
82 }
83
84 LoggerWidget::Visibility LoggerWidget::defaultVisibility() const
85 {
86     return m_visibility;
87 }
88
89 void LoggerWidget::setDefaultVisibility(LoggerWidget::Visibility visibility)
90 {
91     if (m_visibility == visibility)
92         return;
93
94     m_visibility = visibility;
95     m_visibilityOrigin = CommandLineOrigin;
96
97     m_preferencesMenu->setEnabled(m_visibilityOrigin == SettingsOrigin);
98 }
99
100 QMenu *LoggerWidget::preferencesMenu()
101 {
102     return m_preferencesMenu;
103 }
104
105 QAction *LoggerWidget::showAction()
106 {
107     return m_showWidgetAction;
108 }
109
110 void LoggerWidget::readSettings()
111 {
112     QSettings settings;
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;
118     } else {
119         m_visibility = AutoShowWarnings;
120     }
121 }
122
123 void LoggerWidget::saveSettings()
124 {
125     if (m_visibilityOrigin != SettingsOrigin)
126         return;
127
128     QString value = QLatin1String("autoShow");
129     if (defaultVisibility() == ShowWarnings) {
130         value = QLatin1String("show");
131     } else if (defaultVisibility() == HideWarnings) {
132         value = QLatin1String("hide");
133     }
134
135     QSettings settings;
136     settings.setValue(QLatin1String("warnings"), value);
137 }
138
139 void LoggerWidget::warningsPreferenceChanged(QAction *action)
140 {
141     Visibility newSetting = static_cast<Visibility>(action->data().toInt());
142     m_visibility = newSetting;
143     saveSettings();
144 }
145
146 void LoggerWidget::showEvent(QShowEvent *event)
147 {
148     QWidget::showEvent(event);
149     emit opened();
150 }
151
152 void LoggerWidget::closeEvent(QCloseEvent *event)
153 {
154     QWidget::closeEvent(event);
155     emit closed();
156 }
157
158 void LoggerWidget::setupPreferencesMenu()
159 {
160     m_preferencesMenu = new QMenu(tr("Warnings"));
161     QActionGroup *warnings = new QActionGroup(m_preferencesMenu);
162     warnings->setExclusive(true);
163
164     connect(warnings, SIGNAL(triggered(QAction*)), this, SLOT(warningsPreferenceChanged(QAction*)));
165
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);
171
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);
177
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);
183
184     switch (defaultVisibility()) {
185     case LoggerWidget::ShowWarnings:
186         showWarningsPreference->setChecked(true);
187         break;
188     case LoggerWidget::HideWarnings:
189         hideWarningsPreference->setChecked(true);
190         break;
191     default:
192         autoWarningsPreference->setChecked(true);
193     }
194 }
195
196 void LoggerWidget::updateNoWarningsLabel()
197 {
198     m_noWarningsLabel->setVisible(m_plainTextEdit->toPlainText().length() == 0);
199 }
200
201 QT_END_NAMESPACE