OSDN Git Service

32c41523789c56e3dbba8cd6688ea35462a153db
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / sessiondialog.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "sessiondialog.h"
35 #include "session.h"
36
37 #include <QtGui/QInputDialog>
38 #include <QtGui/QValidator>
39
40 using namespace ProjectExplorer;
41 using namespace ProjectExplorer::Internal;
42
43 namespace ProjectExplorer {
44 namespace Internal {
45
46 class SessionValidator : public QValidator
47 {
48 public:
49     SessionValidator(QObject *parent, QStringList sessions);
50     void fixup(QString & input) const;
51     QValidator::State validate(QString & input, int & pos) const;
52 private:
53     QStringList m_sessions;
54 };
55
56 SessionValidator::SessionValidator(QObject *parent, QStringList sessions)
57     : QValidator(parent), m_sessions(sessions)
58 {
59 }
60
61 QValidator::State SessionValidator::validate(QString &input, int &pos) const
62 {
63     Q_UNUSED(pos)
64
65     if (input.contains('/')
66             || input.contains(':')
67             || input.contains('\\')
68             || input.contains('?')
69             || input.contains('*'))
70         return QValidator::Invalid;
71
72     if (m_sessions.contains(input))
73         return QValidator::Intermediate;
74     else
75         return QValidator::Acceptable;
76 }
77
78 void SessionValidator::fixup(QString &input) const
79 {
80     int i = 2;
81     QString copy;
82     do {
83         copy = input + QString(" (%1)").arg(i);
84         ++i;
85     } while (m_sessions.contains(copy));
86     input = copy;
87 }
88
89
90 class SessionNameInputDialog : public QDialog
91 {
92     Q_OBJECT
93 public:
94     SessionNameInputDialog(const QStringList &sessions, QWidget *parent = 0);
95
96     void setValue(const QString &value);
97     QString value() const;
98
99 private:
100     QLineEdit *m_newSessionLineEdit;
101 };
102
103 SessionNameInputDialog::SessionNameInputDialog(const QStringList &sessions, QWidget *parent)
104     : QDialog(parent)
105 {
106     QVBoxLayout *hlayout = new QVBoxLayout(this);
107     QLabel *label = new QLabel(tr("Enter the name of the session:"), this);
108     hlayout->addWidget(label);
109     m_newSessionLineEdit = new QLineEdit(this);
110     m_newSessionLineEdit->setValidator(new SessionValidator(this, sessions));
111     hlayout->addWidget(m_newSessionLineEdit);
112     QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
113     connect(buttons, SIGNAL(accepted()), this, SLOT(accept()));
114     connect(buttons, SIGNAL(rejected()), this, SLOT(reject()));
115     hlayout->addWidget(buttons);
116     setLayout(hlayout);
117 }
118
119 void SessionNameInputDialog::setValue(const QString &value)
120 {
121     m_newSessionLineEdit->setText(value);
122 }
123
124 QString SessionNameInputDialog::value() const
125 {
126     return m_newSessionLineEdit->text();
127 }
128
129
130 SessionDialog::SessionDialog(SessionManager *sessionManager)
131     : m_sessionManager(sessionManager)
132 {
133     m_ui.setupUi(this);
134
135     connect(m_ui.btCreateNew, SIGNAL(clicked()),
136             this, SLOT(createNew()));
137     connect(m_ui.btClone, SIGNAL(clicked()),
138             this, SLOT(clone()));
139     connect(m_ui.btDelete, SIGNAL(clicked()),
140             this, SLOT(remove()));
141
142     connect(m_ui.btSwitch, SIGNAL(clicked()), this, SLOT(switchToSession()));
143     connect(m_ui.btRename, SIGNAL(clicked()), this, SLOT(rename()));
144
145     connect(m_ui.sessionList, SIGNAL(itemDoubleClicked (QListWidgetItem *)),
146             this, SLOT(switchToSession()));
147
148     connect(m_ui.sessionList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
149             this, SLOT(updateActions()));
150
151     m_ui.whatsASessionLabel->setOpenExternalLinks(true);
152     addItems(true);
153     markItems();
154 }
155
156 void SessionDialog::setAutoLoadSession(bool check)
157 {
158     m_ui.autoLoadCheckBox->setChecked(check ? Qt::Checked : Qt::Unchecked);
159 }
160
161 bool SessionDialog::autoLoadSession() const
162 {
163     return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
164 }
165
166
167 void SessionDialog::addItems(bool setDefaultSession)
168 {
169     QStringList sessions = m_sessionManager->sessions();
170     foreach (const QString &session, sessions) {
171         m_ui.sessionList->addItem(session);
172         if (setDefaultSession && session == m_sessionManager->activeSession())
173             m_ui.sessionList->setCurrentRow(m_ui.sessionList->count() - 1);
174     }
175 }
176 void SessionDialog::markItems()
177 {
178     for(int i = 0; i < m_ui.sessionList->count(); ++i) {
179         QListWidgetItem *item = m_ui.sessionList->item(i);
180         QFont f = item->font();
181         QString session = item->data(Qt::DisplayRole).toString();
182         if (m_sessionManager->isDefaultSession(session))
183             f.setItalic(true);
184         else
185             f.setItalic(false);
186         if (m_sessionManager->activeSession() == session && !m_sessionManager->isDefaultVirgin())
187             f.setBold(true);
188         else
189             f.setBold(false);
190         item->setFont(f);
191     }
192 }
193
194 void SessionDialog::updateActions()
195 {
196     if (m_ui.sessionList->currentItem()) {
197         bool isDefault = (m_ui.sessionList->currentItem()->text() == QLatin1String("default"));
198         bool isActive = (m_ui.sessionList->currentItem()->text() == m_sessionManager->activeSession());
199         m_ui.btDelete->setEnabled(!isActive && !isDefault);
200         m_ui.btRename->setEnabled(!isDefault);
201         m_ui.btClone->setEnabled(true);
202         m_ui.btSwitch->setEnabled(true);
203     } else {
204         m_ui.btDelete->setEnabled(false);
205         m_ui.btRename->setEnabled(false);
206         m_ui.btClone->setEnabled(false);
207         m_ui.btSwitch->setEnabled(false);
208     }
209 }
210
211 void SessionDialog::createNew()
212 {
213     SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
214     newSessionInputDialog.setWindowTitle(tr("New session name"));
215
216     if (newSessionInputDialog.exec() == QDialog::Accepted) {
217         QString newSession = newSessionInputDialog.value();
218         if (newSession.isEmpty() || m_sessionManager->sessions().contains(newSession))
219             return;
220
221         m_sessionManager->createSession(newSession);
222         m_ui.sessionList->clear();
223         QStringList sessions = m_sessionManager->sessions();
224         m_ui.sessionList->addItems(sessions);
225         m_ui.sessionList->setCurrentRow(sessions.indexOf(newSession));
226         markItems();
227     }
228 }
229
230 void SessionDialog::clone()
231 {
232     SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
233     newSessionInputDialog.setValue(m_ui.sessionList->currentItem()->text());
234     newSessionInputDialog.setWindowTitle(tr("New session name"));
235
236     if (newSessionInputDialog.exec() == QDialog::Accepted) {
237         QString newSession = newSessionInputDialog.value();
238         if (m_sessionManager->cloneSession(m_ui.sessionList->currentItem()->text(), newSession)) {
239             m_ui.sessionList->clear();
240             QStringList sessions = m_sessionManager->sessions();
241             m_ui.sessionList->addItems(sessions);
242             m_ui.sessionList->setCurrentRow(sessions.indexOf(newSession));
243             markItems();
244         }
245     }
246 }
247
248 void SessionDialog::remove()
249 {
250     m_sessionManager->deleteSession(m_ui.sessionList->currentItem()->text());
251     m_ui.sessionList->clear();
252     addItems(false);
253     markItems();
254 }
255
256 void SessionDialog::rename()
257 {
258     SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
259     newSessionInputDialog.setValue(m_ui.sessionList->currentItem()->text());
260     newSessionInputDialog.setWindowTitle(tr("Rename session"));
261
262     if (newSessionInputDialog.exec() == QDialog::Accepted) {
263         m_sessionManager->renameSession(m_ui.sessionList->currentItem()->text(), newSessionInputDialog.value());
264         m_ui.sessionList->clear();
265         addItems(false);
266         markItems();
267     }
268 }
269
270 void SessionDialog::switchToSession()
271 {
272     QString session = m_ui.sessionList->currentItem()->text();
273     m_sessionManager->loadSession(session);
274     markItems();
275     updateActions();
276 }
277
278 } // namespace Internal
279 } // namespace ProjectExplorer
280
281 #include "sessiondialog.moc"