OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cpaster / settingspage.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
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 #include "settingspage.h"
34 #include "settings.h"
35 #include "cpasterconstants.h"
36
37 #include <utils/qtcassert.h>
38 #include <coreplugin/icore.h>
39
40 #include <QtCore/QSettings>
41 #include <QtCore/QTextStream>
42 #include <QtCore/QCoreApplication>
43
44 namespace CodePaster {
45
46 SettingsWidget::SettingsWidget(const QStringList &protocols, QWidget *parent) :
47     QWidget(parent)
48 {
49     m_ui.setupUi(this);
50     m_ui.defaultProtocol->addItems(protocols);
51 }
52
53 QString SettingsWidget::searchKeywords() const
54 {
55     QString rc;
56     QTextStream(&rc) << m_ui.protocolLabel->text() << ' '
57             << m_ui.userNameLabel->text();
58     rc.remove(QLatin1Char('&'));
59     return rc;
60 }
61
62 void SettingsWidget::setSettings(const Settings &settings)
63 {
64     m_ui.userEdit->setText(settings.username);
65     const int index = m_ui.defaultProtocol->findText(settings.protocol);
66     m_ui.defaultProtocol->setCurrentIndex(index == -1 ? 0  : index);
67     m_ui.clipboardBox->setChecked(settings.copyToClipboard);
68     m_ui.displayBox->setChecked(settings.displayOutput);
69 }
70
71 Settings SettingsWidget::settings()
72 {
73     Settings rc;
74     rc.username = m_ui.userEdit->text();
75     rc.protocol = m_ui.defaultProtocol->currentText();
76     rc.copyToClipboard = m_ui.clipboardBox->isChecked();
77     rc.displayOutput = m_ui.displayBox->isChecked();
78     return rc;
79 }
80
81 SettingsPage::SettingsPage(const QSharedPointer<Settings> &settings) :
82     m_settings(settings), m_widget(0)
83 {
84 }
85
86 SettingsPage::~SettingsPage()
87 {
88 }
89
90 QString SettingsPage::id() const
91 {
92     return QLatin1String("A.General");
93 }
94
95 QString SettingsPage::displayName() const
96 {
97     return tr("General");
98 }
99
100 QString SettingsPage::category() const
101 {
102     return QLatin1String(Constants::CPASTER_SETTINGS_CATEGORY);
103 }
104
105 QString SettingsPage::displayCategory() const
106 {
107     return QCoreApplication::translate("CodePaster", Constants::CPASTER_SETTINGS_TR_CATEGORY);
108 }
109
110 QIcon SettingsPage::categoryIcon() const
111 {
112     return QIcon(QLatin1String(Constants::SETTINGS_CATEGORY_CPASTER_ICON));
113 }
114
115 QWidget *SettingsPage::createPage(QWidget *parent)
116 {
117     m_widget = new SettingsWidget(m_protocols, parent);
118     m_widget->setSettings(*m_settings);
119
120     if (m_searchKeywords.isEmpty())
121         m_searchKeywords = m_widget->searchKeywords();
122     return m_widget;
123 }
124
125 void SettingsPage::apply()
126 {
127     if (!m_widget) // page was never shown
128         return;
129     const Settings newSettings = m_widget->settings();
130     if (newSettings != *m_settings) {
131         *m_settings = newSettings;
132         m_settings->toSettings(Core::ICore::instance()->settings());
133     }
134 }
135
136 bool SettingsPage::matches(const QString &s) const
137 {
138     return m_searchKeywords.contains(s, Qt::CaseInsensitive);
139 }
140
141 void SettingsPage::addProtocol(const QString &name)
142 {
143     m_protocols.append(name);
144 }
145
146 } // namespace CodePaster