tools/qml/loggerwidget.cpp
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "loggerwidget.h"
       
    43 #include <qglobal.h>
       
    44 #include <QDebug>
       
    45 #include <QSettings>
       
    46 #include <QActionGroup>
       
    47 #include <QMenu>
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 LoggerWidget::LoggerWidget(QWidget *parent) :
       
    52     QPlainTextEdit(parent),
       
    53     m_visibilityOrigin(SettingsOrigin)
       
    54 {
       
    55     setAttribute(Qt::WA_QuitOnClose, false);
       
    56     setWindowTitle(tr("Warnings"));
       
    57 
       
    58     readSettings();
       
    59     setupPreferencesMenu();
       
    60 }
       
    61 
       
    62 void LoggerWidget::append(const QString &msg)
       
    63 {
       
    64     appendPlainText(msg);
       
    65 
       
    66     if (!isVisible() && (defaultVisibility() == AutoShowWarnings))
       
    67         setVisible(true);
       
    68 }
       
    69 
       
    70 LoggerWidget::Visibility LoggerWidget::defaultVisibility() const
       
    71 {
       
    72     return m_visibility;
       
    73 }
       
    74 
       
    75 void LoggerWidget::setDefaultVisibility(LoggerWidget::Visibility visibility)
       
    76 {
       
    77     if (m_visibility == visibility)
       
    78         return;
       
    79 
       
    80     m_visibility = visibility;
       
    81     m_visibilityOrigin = CommandLineOrigin;
       
    82 
       
    83     m_preferencesMenu->setEnabled(m_visibilityOrigin == SettingsOrigin);
       
    84 }
       
    85 
       
    86 QMenu *LoggerWidget::preferencesMenu()
       
    87 {
       
    88     return m_preferencesMenu;
       
    89 }
       
    90 
       
    91 QAction *LoggerWidget::showAction()
       
    92 {
       
    93     return m_showWidgetAction;
       
    94 }
       
    95 
       
    96 void LoggerWidget::readSettings()
       
    97 {
       
    98     QSettings settings;
       
    99     QString warningsPreferences = settings.value("warnings", "hide").toString();
       
   100     if (warningsPreferences == "show") {
       
   101         m_visibility = ShowWarnings;
       
   102     } else if (warningsPreferences == "hide") {
       
   103         m_visibility = HideWarnings;
       
   104     } else {
       
   105         m_visibility = AutoShowWarnings;
       
   106     }
       
   107 }
       
   108 
       
   109 void LoggerWidget::saveSettings()
       
   110 {
       
   111     if (m_visibilityOrigin != SettingsOrigin)
       
   112         return;
       
   113 
       
   114     QString value = "autoShow";
       
   115     if (defaultVisibility() == ShowWarnings) {
       
   116         value = "show";
       
   117     } else if (defaultVisibility() == HideWarnings) {
       
   118         value = "hide";
       
   119     }
       
   120 
       
   121     QSettings settings;
       
   122     settings.setValue("warnings", value);
       
   123 }
       
   124 
       
   125 void LoggerWidget::warningsPreferenceChanged(QAction *action)
       
   126 {
       
   127     Visibility newSetting = static_cast<Visibility>(action->data().toInt());
       
   128     m_visibility = newSetting;
       
   129     saveSettings();
       
   130 }
       
   131 
       
   132 void LoggerWidget::showEvent(QShowEvent *event)
       
   133 {
       
   134     QWidget::showEvent(event);
       
   135     emit opened();
       
   136 }
       
   137 
       
   138 void LoggerWidget::closeEvent(QCloseEvent *event)
       
   139 {
       
   140     QWidget::closeEvent(event);
       
   141     emit closed();
       
   142 }
       
   143 
       
   144 void LoggerWidget::setupPreferencesMenu()
       
   145 {
       
   146     m_preferencesMenu = new QMenu(tr("Warnings"));
       
   147     QActionGroup *warnings = new QActionGroup(m_preferencesMenu);
       
   148     warnings->setExclusive(true);
       
   149 
       
   150     connect(warnings, SIGNAL(triggered(QAction*)), this, SLOT(warningsPreferenceChanged(QAction*)));
       
   151 
       
   152     QAction *showWarningsPreference = new QAction(tr("Show by default"), m_preferencesMenu);
       
   153     showWarningsPreference->setCheckable(true);
       
   154     showWarningsPreference->setData(LoggerWidget::ShowWarnings);
       
   155     warnings->addAction(showWarningsPreference);
       
   156     m_preferencesMenu->addAction(showWarningsPreference);
       
   157 
       
   158     QAction *hideWarningsPreference = new QAction(tr("Hide by default"), m_preferencesMenu);
       
   159     hideWarningsPreference->setCheckable(true);
       
   160     hideWarningsPreference->setData(LoggerWidget::HideWarnings);
       
   161     warnings->addAction(hideWarningsPreference);
       
   162     m_preferencesMenu->addAction(hideWarningsPreference);
       
   163 
       
   164     QAction *autoWarningsPreference = new QAction(tr("Show for first warning"), m_preferencesMenu);
       
   165     autoWarningsPreference->setCheckable(true);
       
   166     autoWarningsPreference->setData(LoggerWidget::AutoShowWarnings);
       
   167     warnings->addAction(autoWarningsPreference);
       
   168     m_preferencesMenu->addAction(autoWarningsPreference);
       
   169 
       
   170     switch (defaultVisibility()) {
       
   171     case LoggerWidget::ShowWarnings:
       
   172         showWarningsPreference->setChecked(true);
       
   173         break;
       
   174     case LoggerWidget::HideWarnings:
       
   175         hideWarningsPreference->setChecked(true);
       
   176         break;
       
   177     default:
       
   178         autoWarningsPreference->setChecked(true);
       
   179     }
       
   180 }
       
   181 
       
   182 QT_END_NAMESPACE