1
/* This file is part of the KDE project
3
* Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "katesessionchooser.h"
24
#include "katesessionmanager.h"
25
#include "katesessionchooseritem.h"
26
#include "katedebug.h"
28
#include <KLocalizedString>
29
#include <KStandardGuiItem>
32
#include <QDialogButtonBox>
34
#include <QPushButton>
35
#include <QVBoxLayout>
36
#include <QPushButton>
37
#include <QHeaderView>
39
//BEGIN CHOOSER DIALOG
41
KateSessionChooser::KateSessionChooser(QWidget *parent, const QString &lastSession)
44
setWindowTitle(i18n("Session Chooser"));
45
QVBoxLayout *mainLayout = new QVBoxLayout(this);
47
m_sessions = new QTreeWidget(this);
48
m_sessions->setMinimumSize(400, 200);
49
mainLayout->addWidget(m_sessions);
51
header << i18n("Session Name");
52
header << i18nc("The number of open documents", "Open Documents");
54
m_sessions->setHeaderLabels(header);
55
m_sessions->header()->setStretchLastSection(false);
56
m_sessions->header()->resizeSection(0, (m_sessions->size().width() - 32) * 2 / 3);
57
m_sessions->header()->resizeSection(1, (m_sessions->size().width() - 32) / 3);
58
m_sessions->header()->resizeSection(2, 32);
59
m_sessions->header()->setSectionResizeMode(QHeaderView::Fixed);
60
m_sessions->setRootIsDecorated(false);
61
m_sessions->setItemsExpandable(false);
62
m_sessions->setAllColumnsShowFocus(true);
63
m_sessions->setSelectionBehavior(QAbstractItemView::SelectRows);
64
m_sessions->setSelectionMode(QAbstractItemView::SingleSelection);
66
qCDebug(LOG_KATE) << "Last session is:" << lastSession;
68
KateSessionList slist = KateApp::self()->sessionManager()->sessionList();
69
qSort(slist.begin(), slist.end(), KateSession::compareByName);
71
foreach(const KateSession::Ptr & session, slist) {
72
KateSessionChooserItem *item = new KateSessionChooserItem(m_sessions, session);
73
QPushButton *tmp = new QPushButton(QIcon::fromTheme(QStringLiteral("document")), QString(), m_sessions);
74
QMenu *popup = new QMenu(tmp);
75
QAction *a = popup->addAction(i18n("Clone session settings"));
76
a->setData(QVariant::fromValue((void *)item));
77
connect(a, SIGNAL(triggered()), this, SLOT(slotCopySession()));
78
a = popup->addAction(i18n("Delete this session"));
79
a->setData(QVariant::fromValue((void *)item));
80
connect(a, SIGNAL(triggered()), this, SLOT(slotDeleteSession()));
82
m_sessions->setItemWidget(item, 2, tmp);
84
if (session->name() == lastSession) {
85
m_sessions->setCurrentItem(item);
89
connect(m_sessions, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(selectionChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
90
connect(m_sessions, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotOpen()));
93
QHBoxLayout *hb = new QHBoxLayout();
95
mainLayout->addLayout(hb);
97
m_useLast = new QCheckBox(i18n("&Always use this choice"), this);
98
hb->addWidget(m_useLast);
101
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
102
hb->addWidget(buttonBox);
104
QPushButton *cancelButton = new QPushButton();
105
KGuiItem::assign(cancelButton, KStandardGuiItem::quit());
106
connect(cancelButton, SIGNAL(clicked()), this, SLOT(slotCancel()));
107
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
109
m_openButton = new QPushButton(QIcon::fromTheme(QStringLiteral("document-open")), i18n("Open Session"));
110
m_openButton->setEnabled(m_sessions->currentIndex().isValid());
111
m_openButton->setDefault(true);
112
m_openButton->setFocus();
113
buttonBox->addButton(m_openButton, QDialogButtonBox::ActionRole);
114
connect(m_openButton, SIGNAL(clicked()), this, SLOT(slotOpen()));
116
QPushButton *newButton = new QPushButton(QIcon::fromTheme(QStringLiteral("document-new")), i18n("New Session"));
117
buttonBox->addButton(newButton, QDialogButtonBox::ActionRole);
118
connect(newButton, SIGNAL(clicked()), this, SLOT(slotNew()));
120
setResult(resultNone);
121
//m_sessions->resizeColumnToContents(0);
122
selectionChanged(NULL, NULL);
125
void KateSessionChooser::resizeEvent(QResizeEvent *)
127
m_sessions->header()->resizeSection(0, (m_sessions->size().width() - 32) * 2 / 3);
128
m_sessions->header()->resizeSection(1, (m_sessions->size().width() - 32) / 3);
129
m_sessions->header()->resizeSection(2, 32);
132
KateSessionChooser::~KateSessionChooser()
135
void KateSessionChooser::slotCopySession()
137
m_sessions->setCurrentItem((KateSessionChooserItem *)((QAction *)sender())->data().value<void *>());
138
Q_ASSERT(static_cast<KateSessionChooserItem *>(m_sessions->currentItem()));
142
void KateSessionChooser::slotDeleteSession()
144
KateSessionChooserItem *item = (KateSessionChooserItem *)((QAction *)sender())->data().value<void *>();
149
KateApp::self()->sessionManager()->deleteSession(item->session);
150
m_sessions->removeItemWidget(item, 2);
155
KateSession::Ptr KateSessionChooser::selectedSession()
157
KateSessionChooserItem *item = static_cast<KateSessionChooserItem *>(m_sessions->currentItem());
159
Q_ASSERT(item || ((result() != resultOpen) && (result() != resultCopy)));
162
return KateSession::Ptr();
165
return item->session;
168
bool KateSessionChooser::reopenLastSession()
170
return m_useLast->isChecked();
173
void KateSessionChooser::slotOpen()
175
Q_ASSERT(static_cast<KateSessionChooserItem *>(m_sessions->currentItem()));
179
void KateSessionChooser::slotNew()
184
void KateSessionChooser::slotCancel()
189
void KateSessionChooser::selectionChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
192
m_openButton->setEnabled(true);