~ubuntu-branches/ubuntu/wily/zanshin/wily-proposed

« back to all changes in this revision

Viewing changes to src/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2009-08-13 23:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20090813231932-lewqphiry1qs7w80
Tags: upstream-0.1+svn1006410
ImportĀ upstreamĀ versionĀ 0.1+svn1006410

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Zanshin Todo.
 
2
 
 
3
   Copyright 2008 Kevin Ottens <ervin@kde.org>
 
4
   Copyright 2008, 2009 Mario Bensi <nef@ipsquad.net>
 
5
 
 
6
   This program is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU General Public License as
 
8
   published by the Free Software Foundation; either version 2 of
 
9
   the License or (at your option) version 3 or any later version
 
10
   accepted by the membership of KDE e.V. (or its successor approved
 
11
   by the membership of KDE e.V.), which shall act as a proxy
 
12
   defined in Section 14 of version 3 of the license.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program; if not, write to the Free Software
 
21
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 
22
   USA.
 
23
*/
 
24
 
 
25
#include "mainwindow.h"
 
26
 
 
27
#include <akonadi/control.h>
 
28
 
 
29
#include <akonadi/collectionfetchjob.h>
 
30
 
 
31
#include <KDE/KAction>
 
32
#include <KDE/KActionCollection>
 
33
#include <KDE/KConfigGroup>
 
34
#include <KDE/KIcon>
 
35
#include <KDE/KLocale>
 
36
 
 
37
#include <QtGui/QDockWidget>
 
38
#include <QtGui/QHeaderView>
 
39
 
 
40
#include "actionlisteditor.h"
 
41
#include "actionlistview.h"
 
42
#include "configdialog.h"
 
43
#include "globalmodel.h"
 
44
#include "globalsettings.h"
 
45
#include "todoflatmodel.h"
 
46
#include "sidebar.h"
 
47
 
 
48
MainWindow::MainWindow(QWidget *parent)
 
49
    : KXmlGuiWindow(parent)
 
50
{
 
51
    Akonadi::Control::start();
 
52
 
 
53
    setupSideBar();
 
54
    setupCentralWidget();
 
55
    setupActions();
 
56
 
 
57
    setupGUI();
 
58
 
 
59
    restoreColumnState();
 
60
    applySettings();
 
61
 
 
62
    actionCollection()->action("project_mode")->trigger();
 
63
}
 
64
 
 
65
void MainWindow::setupCentralWidget()
 
66
{
 
67
    m_editor = new ActionListEditor(this, actionCollection());
 
68
 
 
69
    connect(m_sidebar, SIGNAL(noProjectInboxActivated()),
 
70
            m_editor, SLOT(showNoProjectInbox()));
 
71
    connect(m_sidebar, SIGNAL(projectActivated(QModelIndex)),
 
72
            m_editor, SLOT(focusOnProject(QModelIndex)));
 
73
    connect(m_sidebar, SIGNAL(noContextInboxActivated()),
 
74
            m_editor, SLOT(showNoContextInbox()));
 
75
    connect(m_sidebar, SIGNAL(contextActivated(QModelIndex)),
 
76
            m_editor, SLOT(focusOnContext(QModelIndex)));
 
77
 
 
78
    setCentralWidget(m_editor);
 
79
}
 
80
 
 
81
void MainWindow::setupSideBar()
 
82
{
 
83
    m_sidebar = new SideBar(this, actionCollection());
 
84
 
 
85
    QDockWidget *dock = new QDockWidget(this);
 
86
    dock->setObjectName("SideBar");
 
87
    dock->setFeatures(dock->features() & ~QDockWidget::DockWidgetClosable);
 
88
    dock->setWidget(m_sidebar);
 
89
    addDockWidget(Qt::LeftDockWidgetArea, dock);
 
90
}
 
91
 
 
92
void MainWindow::setupActions()
 
93
{
 
94
    KActionCollection *ac = actionCollection();
 
95
 
 
96
    QActionGroup *modeGroup = new QActionGroup(this);
 
97
    modeGroup->setExclusive(true);
 
98
 
 
99
    KAction *action = ac->addAction("project_mode", m_sidebar, SLOT(switchToProjectMode()));
 
100
    action->setText(i18n("Project View"));
 
101
    action->setIcon(KIcon("view-pim-tasks"));
 
102
    action->setShortcut(Qt::CTRL | Qt::Key_P);
 
103
    action->setCheckable(true);
 
104
    modeGroup->addAction(action);
 
105
 
 
106
    action = ac->addAction("context_mode", m_sidebar, SLOT(switchToContextMode()));
 
107
    action->setText(i18n("Context View"));
 
108
    action->setIcon(KIcon("view-pim-notes"));
 
109
    action->setShortcut(Qt::CTRL | Qt::Key_O);
 
110
    action->setCheckable(true);
 
111
    modeGroup->addAction(action);
 
112
 
 
113
    ac->addAction(KStandardAction::Preferences, this, SLOT(showConfigDialog()));
 
114
    ac->addAction(KStandardAction::Quit, this, SLOT(close()));
 
115
}
 
116
 
 
117
void MainWindow::closeEvent(QCloseEvent *event)
 
118
{
 
119
    saveColumnsState();
 
120
    KXmlGuiWindow::closeEvent(event);
 
121
}
 
122
 
 
123
void MainWindow::saveAutoSaveSettings()
 
124
{
 
125
    saveColumnsState();
 
126
    KXmlGuiWindow::saveAutoSaveSettings();
 
127
}
 
128
 
 
129
void MainWindow::saveColumnsState()
 
130
{
 
131
    KConfigGroup cg = autoSaveConfigGroup();
 
132
    QByteArray state = m_editor->view()->header()->saveState();
 
133
    cg.writeEntry("MainHeaderState", state.toBase64());
 
134
}
 
135
 
 
136
void MainWindow::restoreColumnState()
 
137
{
 
138
    KConfigGroup cg = autoSaveConfigGroup();
 
139
    QByteArray state;
 
140
    if (cg.hasKey("MainHeaderState")) {
 
141
        state = cg.readEntry("MainHeaderState", state);
 
142
        m_editor->view()->header()->restoreState(QByteArray::fromBase64(state));
 
143
    }
 
144
}
 
145
 
 
146
void MainWindow::showConfigDialog()
 
147
{
 
148
    if (KConfigDialog::showDialog("settings")) {
 
149
        return;
 
150
    }
 
151
 
 
152
    ConfigDialog *dialog = new ConfigDialog(this, "settings",
 
153
                                            GlobalSettings::self());
 
154
 
 
155
    connect(dialog, SIGNAL(settingsChanged(const QString&)),
 
156
            this, SLOT(applySettings()));
 
157
 
 
158
    dialog->show();
 
159
}
 
160
 
 
161
void MainWindow::applySettings()
 
162
{
 
163
    Akonadi::Collection collection(GlobalSettings::collectionId());
 
164
 
 
165
    if (!collection.isValid()) {
 
166
        showConfigDialog();
 
167
        return;
 
168
    }
 
169
 
 
170
    Akonadi::CollectionFetchJob *job =  new Akonadi::CollectionFetchJob(collection, Akonadi::CollectionFetchJob::Base);
 
171
    job->exec();
 
172
 
 
173
    if (job->collections().isEmpty()) {
 
174
        showConfigDialog();
 
175
        return;
 
176
    }
 
177
 
 
178
    GlobalModel::todoFlat()->setCollection(job->collections().first());
 
179
}