~ubuntu-branches/ubuntu/vivid/kate/vivid-proposed

« back to all changes in this revision

Viewing changes to kate/src/session/katesessionchooser.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
 *
 
3
 *  Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 */
 
20
 
 
21
#include "katesessionchooser.h"
 
22
 
 
23
#include "kateapp.h"
 
24
#include "katesessionmanager.h"
 
25
#include "katesessionchooseritem.h"
 
26
#include "katedebug.h"
 
27
 
 
28
#include <KLocalizedString>
 
29
#include <KStandardGuiItem>
 
30
 
 
31
#include <QCheckBox>
 
32
#include <QDialogButtonBox>
 
33
#include <QMenu>
 
34
#include <QPushButton>
 
35
#include <QVBoxLayout>
 
36
#include <QPushButton>
 
37
#include <QHeaderView>
 
38
 
 
39
//BEGIN CHOOSER DIALOG
 
40
 
 
41
KateSessionChooser::KateSessionChooser(QWidget *parent, const QString &lastSession)
 
42
    : QDialog(parent)
 
43
{
 
44
    setWindowTitle(i18n("Session Chooser"));
 
45
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
 
46
 
 
47
    m_sessions = new QTreeWidget(this);
 
48
    m_sessions->setMinimumSize(400, 200);
 
49
    mainLayout->addWidget(m_sessions);
 
50
    QStringList header;
 
51
    header << i18n("Session Name");
 
52
    header << i18nc("The number of open documents", "Open Documents");
 
53
    header << QString();
 
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);
 
65
 
 
66
    qCDebug(LOG_KATE) << "Last session is:" << lastSession;
 
67
 
 
68
    KateSessionList slist = KateApp::self()->sessionManager()->sessionList();
 
69
    qSort(slist.begin(), slist.end(), KateSession::compareByName);
 
70
 
 
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()));
 
81
        tmp->setMenu(popup);
 
82
        m_sessions->setItemWidget(item, 2, tmp);
 
83
 
 
84
        if (session->name() == lastSession) {
 
85
            m_sessions->setCurrentItem(item);
 
86
        }
 
87
    }
 
88
 
 
89
    connect(m_sessions, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(selectionChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
 
90
    connect(m_sessions, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotOpen()));
 
91
 
 
92
    // bottom box
 
93
    QHBoxLayout *hb = new QHBoxLayout();
 
94
    hb->setMargin(0);
 
95
    mainLayout->addLayout(hb);
 
96
 
 
97
    m_useLast = new QCheckBox(i18n("&Always use this choice"), this);
 
98
    hb->addWidget(m_useLast);
 
99
 
 
100
    // buttons
 
101
    QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
 
102
    hb->addWidget(buttonBox);
 
103
 
 
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);
 
108
 
 
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()));
 
115
 
 
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()));
 
119
 
 
120
    setResult(resultNone);
 
121
    //m_sessions->resizeColumnToContents(0);
 
122
    selectionChanged(NULL, NULL);
 
123
}
 
124
 
 
125
void KateSessionChooser::resizeEvent(QResizeEvent *)
 
126
{
 
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);
 
130
}
 
131
 
 
132
KateSessionChooser::~KateSessionChooser()
 
133
{}
 
134
 
 
135
void KateSessionChooser::slotCopySession()
 
136
{
 
137
    m_sessions->setCurrentItem((KateSessionChooserItem *)((QAction *)sender())->data().value<void *>());
 
138
    Q_ASSERT(static_cast<KateSessionChooserItem *>(m_sessions->currentItem()));
 
139
    done(resultCopy);
 
140
}
 
141
 
 
142
void KateSessionChooser::slotDeleteSession()
 
143
{
 
144
    KateSessionChooserItem *item = (KateSessionChooserItem *)((QAction *)sender())->data().value<void *>();
 
145
    if (!item) {
 
146
        return;
 
147
    }
 
148
 
 
149
    KateApp::self()->sessionManager()->deleteSession(item->session);
 
150
    m_sessions->removeItemWidget(item, 2);
 
151
    delete item;
 
152
 
 
153
}
 
154
 
 
155
KateSession::Ptr KateSessionChooser::selectedSession()
 
156
{
 
157
    KateSessionChooserItem *item = static_cast<KateSessionChooserItem *>(m_sessions->currentItem());
 
158
 
 
159
    Q_ASSERT(item || ((result() != resultOpen) && (result() != resultCopy)));
 
160
 
 
161
    if (!item) {
 
162
        return KateSession::Ptr();
 
163
    }
 
164
 
 
165
    return item->session;
 
166
}
 
167
 
 
168
bool KateSessionChooser::reopenLastSession()
 
169
{
 
170
    return m_useLast->isChecked();
 
171
}
 
172
 
 
173
void KateSessionChooser::slotOpen()
 
174
{
 
175
    Q_ASSERT(static_cast<KateSessionChooserItem *>(m_sessions->currentItem()));
 
176
    done(resultOpen);
 
177
}
 
178
 
 
179
void KateSessionChooser::slotNew()
 
180
{
 
181
    done(resultNew);
 
182
}
 
183
 
 
184
void KateSessionChooser::slotCancel()
 
185
{
 
186
    done(resultQuit);
 
187
}
 
188
 
 
189
void KateSessionChooser::selectionChanged(QTreeWidgetItem *current, QTreeWidgetItem *)
 
190
{
 
191
    Q_UNUSED(current);
 
192
    m_openButton->setEnabled(true);
 
193
}
 
194
 
 
195
//END CHOOSER DIALOG
 
196