File: taskjuggler.cpp

package info (click to toggle)
taskjuggler 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 13,252 kB
  • ctags: 3,649
  • sloc: cpp: 37,290; sh: 12,764; xml: 5,938; perl: 5,207; makefile: 282; python: 258; lisp: 67
file content (371 lines) | stat: -rw-r--r-- 10,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * The TaskJuggler Project Management Software
 *
 * Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 *               by Chris Schlaeger <cs@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * $Id$
 */


#include "taskjuggler.h"
#include "pref.h"

#include <qdragobject.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qtimer.h>
#include <qstringlist.h>

#include <kglobal.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kdeversion.h>
#include <kmenubar.h>
#include <kstatusbar.h>
#include <kkeydialog.h>
#include <kfiledialog.h>
#include <kfileitem.h>
#include <kaccel.h>
#include <kio/netaccess.h>
#include <kconfig.h>
#include <kstandarddirs.h>
#include <kurldrag.h>
#include <kurlrequesterdlg.h>
#include <kedittoolbar.h>
#include <kstdaccel.h>
#include <kaction.h>
#include <kactioncollection.h>
#include <kstdaction.h>
#include <kdebug.h>
#include <kprinter.h>
#include <kmessagebox.h>
#include <ktip.h>

TaskJuggler::TaskJuggler()
    : KMainWindow( 0, "TaskJuggler" ),
      m_view(new TaskJugglerView(this))
{
    // accept dnd
    setAcceptDrops(true);

    // tell the KMainWindow that this is indeed the main widget
    setCentralWidget(m_view);

    // then, setup our actions
    setupActions();

    // and a status bar
    statusBar()->show();

    // allow the view to change the statusbar and caption
    connect(m_view, SIGNAL(signalChangeStatusbar(const QString&)),
            this,   SLOT(changeStatusbar(const QString&)));
    connect(m_view, SIGNAL(signalChangeCaption(const QString&)),
            this,   SLOT(changeCaption(const QString&)));
    connect(m_view, SIGNAL(announceRecentURL(const KURL&)),
            this, SLOT(addRecentURL(const KURL&)));

    /* Add the taskjuggler apps dir as additional resource so the katepart
     * will find the katefiletyperc file in there. */
    KGlobal().dirs()->addResourceDir
        ("config", KGlobal().dirs()->findResourceDir
         ("data", "taskjuggler/katefiletyperc") + "/taskjuggler/");

    readProperties(kapp->config());

    delayTimer = new QTimer(this);
    connect(delayTimer, SIGNAL(timeout()),
            this, SLOT(showTipOnStart()));
    delayTimer->start(200, true);
}

TaskJuggler::~TaskJuggler()
{
}

void
TaskJuggler::load(const KURL& fileURL)
{
    if (KFileItem(fileURL, "application/x-tjp",
                           KFileItem::Unknown).size() == 0)
    {
        if (KMessageBox::warningContinueCancel
            (this, i18n("The project file '%1' cannot be opened. If you "
                        "continue, a new file with this name will be "
                        "created.").arg(fileURL.url()), i18n("File not found"),
             KStdGuiItem::cont(), "WarnOnNewFileCreation") ==
            KMessageBox::Cancel)
            return;

        // Create a new project with the specified name.`
        m_view->newProject(fileURL);
    }
    else
        m_view->openURL(fileURL);
}

void TaskJuggler::setupActions()
{
    // "File" menu
    KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
    new KAction(i18n("New &Include File" ), "file_temporary", KShortcut(),
                this, SLOT(fileNewInclude()),
                actionCollection(), "new_include");
    KStdAction::open(this, SLOT(fileOpen()), actionCollection());
    KStdAction::close(this, SLOT(fileClose()), actionCollection());
    KStdAction::print(this, SLOT(filePrint()), actionCollection());
    KStdAction::quit(kapp, SLOT(closeAllWindows()), actionCollection());

    // Setup "Open Recent" menu and load old recent files.
    m_recentAction = KStdAction::openRecent(this, SLOT(load(const KURL&)),
                                            actionCollection());


    // "Goto" menu
    new KAction(i18n("Tas&ks"), "tj_task_group", KShortcut(KKey("ALT+k")),
                m_view, SLOT(setFocusToTaskList()),
                actionCollection(), "tasks");
    new KAction(i18n("&Resources"), "tj_resource_group",
                KShortcut(KKey("ALT+r")),
                m_view, SLOT(setFocusToResourceList()),
                actionCollection(), "resources");
    new KAction(i18n("&Accounts"), "tj_account_group",
                KShortcut(KKey("ALT+a")),
                m_view, SLOT(setFocusToAccountList()),
                actionCollection(), "accounts");
    new KAction(i18n("Re&ports"), "tj_report_list", KShortcut(KKey("ALT+p")),
                m_view, SLOT(setFocusToReportList()),
                actionCollection(), "reports");
    new KAction(i18n("F&iles"), "tj_file_list", KShortcut(KKey("ALT+i")),
                m_view, SLOT(setFocusToFileList()),
                actionCollection(), "files");
    new KAction(i18n("E&ditor"), "tj_editor", KShortcut("ALT+d"),
                m_view, SLOT(setFocusToEditor()),
                actionCollection(), "editor");
    new KAction(i18n("Rep&ort"), "tj_report", KShortcut("ALT+o"),
                m_view, SLOT(setFocusToReport()),
                actionCollection(), "report");

    // "Tools" menu
    new KAction(i18n("&Schedule"), "tj_schedule", KShortcut(KKey("F9")),
                m_view, SLOT(schedule()),
                actionCollection(), "schedule");
    new KAction(i18n("Stop scheduling"), "stop", 0,
                m_view, SLOT(stop()),
                actionCollection(), "stop");
    new KAction(i18n("&Generate all Reports"), 0, 0,
                m_view, SLOT(generate()),
                actionCollection(), "generate");
    new KAction(i18n("Goto &previous Problem"), "tj_previous_problem",
                KShortcut(KKey("F10")),
                m_view, SLOT(previousProblem()),
                actionCollection(), "previous_problem");
    new KAction(i18n("Goto &next Problem"), "tj_next_problem",
                KShortcut(KKey("F11")),
                m_view, SLOT(nextProblem()),
                actionCollection(), "next_problem");

    new KAction(i18n("Zoom &In"), "viewmag+", KShortcut(KKey("F7")),
                m_view, SLOT(zoomIn()),
                actionCollection(), "zoom_in");
    new KAction(i18n("Zoom &Out"), "viewmag-", KShortcut(KKey("F8")),
                m_view, SLOT(zoomOut()),
                actionCollection(), "zoom_out");

    // "Help" menu
    new KAction(i18n("Tip of the day"), "idea", 0, this,
                SLOT(showTip()), actionCollection(), "tip");
    new KAction(i18n("Explain Keyword"), "tj_keyword_help",
                KShortcut(KKey("F2")),
                m_view, SLOT(keywordHelp()),
                actionCollection(), "keyword_help");
    new KAction(i18n("Tutorial"), "tj_tutorial", 0,
                m_view, SLOT(tutorial()),
                actionCollection(), "tutorial");

    setupGUI(ToolBar | Keys | StatusBar | Save | Create);
}

void
TaskJuggler::enableActions(bool enable)
{
    if (!enable)
        enabledActionsBuf.clear();

    KActionPtrList actionList = actionCollection()->actions();
    for (KActionPtrList::iterator it = actionList.begin();
         it != actionList.end(); ++it)
    {
        /* The "stop" action will be handled opposite to all other actions. */
        if (strcmp((*it)->name(), "stop") == 0)
        {
            (*it)->setEnabled(!enable);
            continue;
        }

        if (enable)
        {
            if (enabledActionsBuf.find((*it)->name()) !=
                enabledActionsBuf.end())
                (*it)->setEnabled(true);
        }
        else
        {
            if ((*it)->isEnabled())
            {
                enabledActionsBuf.insert((*it)->name());
                (*it)->setEnabled(false);
            }
        }
    }
}

void
TaskJuggler::saveProperties(KConfig* config)
{
    config->setGroup("Global Settings");

    m_view->saveProperties(config);

    m_recentAction->saveEntries(config);
}

void
TaskJuggler::readProperties(KConfig* config)
{
    config->setGroup("Global Settings");
    lastURL = config->readPathEntry("lastURL");

    m_recentAction->loadEntries(config);

    m_view->readProperties(config);
}

void
TaskJuggler::loadLastProject()
{
    if (!lastURL.isEmpty())
        m_view->openURL(KURL(lastURL));
}

void
TaskJuggler::dragEnterEvent(QDragEnterEvent *event)
{
    // accept uri drops only
    event->accept(KURLDrag::canDecode(event));
}

void
TaskJuggler::dropEvent(QDropEvent *event)
{
    // this is a very simplistic implementation of a drop event.  we
    // will only accept a dropped URL.  the Qt dnd code can do *much*
    // much more, so please read the docs there
    KURL::List urls;

    // see if we can decode a URI.. if not, just ignore it
    if (KURLDrag::decode(event, urls) && !urls.isEmpty())
    {
        // okay, we have a URI.. process it
        const KURL &url = urls.first();

        // load in the file
        load(url);
    }
}

bool
TaskJuggler::queryClose()
{
    return true;
}

bool
TaskJuggler::queryExit()
{
    m_view->quit(false);
    saveProperties(kapp->config());
    return true;
}

void
TaskJuggler::fileNew()
{
    m_view->newProject();
}

void
TaskJuggler::fileNewInclude()
{
    m_view->newInclude();
}

void
TaskJuggler::fileOpen()
{
    m_view->openURL(KURL());
}

void
TaskJuggler::fileClose()
{
    m_view->close();
}

void
TaskJuggler::filePrint()
{
    m_view->print();
}

void
TaskJuggler::optionsPreferences()
{
    // popup some sort of preference dialog, here
    TaskJugglerPreferences dlg;
    if (dlg.exec())
    {
        // redo your settings
    }
}

void
TaskJuggler::showTip()
{
    KTipDialog::showTip(this, QString::null, true);
}

void
TaskJuggler::showTipOnStart()
{
    delete delayTimer;
    delayTimer = 0;

    KTipDialog::showTip(this);
}

void TaskJuggler::changeStatusbar(const QString& text)
{
    // display the text on the statusbar
    statusBar()->message(text);
}

void TaskJuggler::changeCaption(const QString& text)
{
    // display the text on the caption
    setCaption(text);
}

void
TaskJuggler::addRecentURL(const KURL& url)
{
    m_recentAction->addURL(url);
    setCaption(url.prettyURL());
}

#include "taskjuggler.moc"