1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "sessiondialog.h"
37 #include <QtGui/QInputDialog>
38 #include <QtGui/QValidator>
40 using namespace ProjectExplorer;
41 using namespace ProjectExplorer::Internal;
43 namespace ProjectExplorer {
46 class SessionValidator : public QValidator
49 SessionValidator(QObject *parent, QStringList sessions);
50 void fixup(QString & input) const;
51 QValidator::State validate(QString & input, int & pos) const;
53 QStringList m_sessions;
56 SessionValidator::SessionValidator(QObject *parent, QStringList sessions)
57 : QValidator(parent), m_sessions(sessions)
61 QValidator::State SessionValidator::validate(QString &input, int &pos) const
65 if (input.contains('/')
66 || input.contains(':')
67 || input.contains('\\')
68 || input.contains('?')
69 || input.contains('*'))
70 return QValidator::Invalid;
72 if (m_sessions.contains(input))
73 return QValidator::Intermediate;
75 return QValidator::Acceptable;
78 void SessionValidator::fixup(QString &input) const
83 copy = input + QString(" (%1)").arg(i);
85 } while (m_sessions.contains(copy));
90 class SessionNameInputDialog : public QDialog
94 SessionNameInputDialog(const QStringList &sessions, QWidget *parent = 0);
96 void setValue(const QString &value);
97 QString value() const;
100 QLineEdit *m_newSessionLineEdit;
103 SessionNameInputDialog::SessionNameInputDialog(const QStringList &sessions, QWidget *parent)
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);
119 void SessionNameInputDialog::setValue(const QString &value)
121 m_newSessionLineEdit->setText(value);
124 QString SessionNameInputDialog::value() const
126 return m_newSessionLineEdit->text();
130 SessionDialog::SessionDialog(SessionManager *sessionManager)
131 : m_sessionManager(sessionManager)
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()));
142 connect(m_ui.btSwitch, SIGNAL(clicked()), this, SLOT(switchToSession()));
143 connect(m_ui.btRename, SIGNAL(clicked()), this, SLOT(rename()));
145 connect(m_ui.sessionList, SIGNAL(itemDoubleClicked (QListWidgetItem *)),
146 this, SLOT(switchToSession()));
148 connect(m_ui.sessionList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
149 this, SLOT(updateActions()));
151 m_ui.whatsASessionLabel->setOpenExternalLinks(true);
156 void SessionDialog::setAutoLoadSession(bool check)
158 m_ui.autoLoadCheckBox->setChecked(check ? Qt::Checked : Qt::Unchecked);
161 bool SessionDialog::autoLoadSession() const
163 return m_ui.autoLoadCheckBox->checkState() == Qt::Checked;
167 void SessionDialog::addItems(bool setDefaultSession)
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);
176 void SessionDialog::markItems()
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))
186 if (m_sessionManager->activeSession() == session && !m_sessionManager->isDefaultVirgin())
194 void SessionDialog::updateActions()
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);
204 m_ui.btDelete->setEnabled(false);
205 m_ui.btRename->setEnabled(false);
206 m_ui.btClone->setEnabled(false);
207 m_ui.btSwitch->setEnabled(false);
211 void SessionDialog::createNew()
213 SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
214 newSessionInputDialog.setWindowTitle(tr("New session name"));
216 if (newSessionInputDialog.exec() == QDialog::Accepted) {
217 QString newSession = newSessionInputDialog.value();
218 if (newSession.isEmpty() || m_sessionManager->sessions().contains(newSession))
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));
230 void SessionDialog::clone()
232 SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
233 newSessionInputDialog.setValue(m_ui.sessionList->currentItem()->text());
234 newSessionInputDialog.setWindowTitle(tr("New session name"));
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));
248 void SessionDialog::remove()
250 m_sessionManager->deleteSession(m_ui.sessionList->currentItem()->text());
251 m_ui.sessionList->clear();
256 void SessionDialog::rename()
258 SessionNameInputDialog newSessionInputDialog(m_sessionManager->sessions(), this);
259 newSessionInputDialog.setValue(m_ui.sessionList->currentItem()->text());
260 newSessionInputDialog.setWindowTitle(tr("Rename session"));
262 if (newSessionInputDialog.exec() == QDialog::Accepted) {
263 m_sessionManager->renameSession(m_ui.sessionList->currentItem()->text(), newSessionInputDialog.value());
264 m_ui.sessionList->clear();
270 void SessionDialog::switchToSession()
272 QString session = m_ui.sessionList->currentItem()->text();
273 m_sessionManager->loadSession(session);
278 } // namespace Internal
279 } // namespace ProjectExplorer
281 #include "sessiondialog.moc"