Initial import from the monolithic Qt.
[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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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 #ifdef Q_WS_MAEMO_5
51 #  include <QScrollArea>
52 #  include <QVBoxLayout>
53 #  include "texteditautoresizer_maemo5.h"
54 #endif
55
56 #include "loggerwidget.h"
57
58 QT_BEGIN_NAMESPACE
59
60 LoggerWidget::LoggerWidget(QWidget *parent) :
61     QMainWindow(parent),
62     m_visibilityOrigin(SettingsOrigin)
63 {
64     setAttribute(Qt::WA_QuitOnClose, false);
65     setWindowTitle(tr("Warnings"));
66
67     m_plainTextEdit = new QPlainTextEdit();
68
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);
74
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 );
80 #endif
81
82 #ifdef Q_WS_MAEMO_5
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);
89 #else
90     setCentralWidget(m_plainTextEdit);
91 #endif
92
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()));
100
101     readSettings();
102     setupPreferencesMenu();
103 }
104
105 void LoggerWidget::append(const QString &msg)
106 {
107     m_plainTextEdit->appendPlainText(msg);
108
109     if (!isVisible() && (defaultVisibility() == AutoShowWarnings))
110         setVisible(true);
111 }
112
113 LoggerWidget::Visibility LoggerWidget::defaultVisibility() const
114 {
115     return m_visibility;
116 }
117
118 void LoggerWidget::setDefaultVisibility(LoggerWidget::Visibility visibility)
119 {
120     if (m_visibility == visibility)
121         return;
122
123     m_visibility = visibility;
124     m_visibilityOrigin = CommandLineOrigin;
125
126     m_preferencesMenu->setEnabled(m_visibilityOrigin == SettingsOrigin);
127 }
128
129 QMenu *LoggerWidget::preferencesMenu()
130 {
131     return m_preferencesMenu;
132 }
133
134 QAction *LoggerWidget::showAction()
135 {
136     return m_showWidgetAction;
137 }
138
139 void LoggerWidget::readSettings()
140 {
141     QSettings settings;
142     QString warningsPreferences = settings.value("warnings", "hide").toString();
143     if (warningsPreferences == "show") {
144         m_visibility = ShowWarnings;
145     } else if (warningsPreferences == "hide") {
146         m_visibility = HideWarnings;
147     } else {
148         m_visibility = AutoShowWarnings;
149     }
150 }
151
152 void LoggerWidget::saveSettings()
153 {
154     if (m_visibilityOrigin != SettingsOrigin)
155         return;
156
157     QString value = "autoShow";
158     if (defaultVisibility() == ShowWarnings) {
159         value = "show";
160     } else if (defaultVisibility() == HideWarnings) {
161         value = "hide";
162     }
163
164     QSettings settings;
165     settings.setValue("warnings", value);
166 }
167
168 void LoggerWidget::warningsPreferenceChanged(QAction *action)
169 {
170     Visibility newSetting = static_cast<Visibility>(action->data().toInt());
171     m_visibility = newSetting;
172     saveSettings();
173 }
174
175 void LoggerWidget::showEvent(QShowEvent *event)
176 {
177     QWidget::showEvent(event);
178     emit opened();
179 }
180
181 void LoggerWidget::closeEvent(QCloseEvent *event)
182 {
183     QWidget::closeEvent(event);
184     emit closed();
185 }
186
187 void LoggerWidget::setupPreferencesMenu()
188 {
189     m_preferencesMenu = new QMenu(tr("Warnings"));
190     QActionGroup *warnings = new QActionGroup(m_preferencesMenu);
191     warnings->setExclusive(true);
192
193     connect(warnings, SIGNAL(triggered(QAction*)), this, SLOT(warningsPreferenceChanged(QAction*)));
194
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);
200
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);
206
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);
212
213     switch (defaultVisibility()) {
214     case LoggerWidget::ShowWarnings:
215         showWarningsPreference->setChecked(true);
216         break;
217     case LoggerWidget::HideWarnings:
218         hideWarningsPreference->setChecked(true);
219         break;
220     default:
221         autoWarningsPreference->setChecked(true);
222     }
223 }
224
225 void LoggerWidget::updateNoWarningsLabel()
226 {
227     m_noWarningsLabel->setVisible(m_plainTextEdit->toPlainText().length() == 0);
228 }
229
230 QT_END_NAMESPACE