Documentation: Completed Backup section, update menu.
[kdepim.git] / ktimetracker / mainwindow.cpp
blob80c732561d57902868f14b5d8a8172f90eccad18
1 /*
2 * Copyright (C) 2003 by Scott Monachello <smonach@cox.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the
16 * Free Software Foundation, Inc.
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301 USA.
22 #include "mainwindow.h"
24 #include <numeric>
26 #include <QMenu>
27 #include <QString>
28 #include <QTimer>
30 #include <KAction>
31 #include <KApplication> // kapp
32 #include <KDebug>
33 #include <KGlobal>
34 #include <KIcon>
35 #include <KLocale> // i18n
36 #include <KMessageBox>
37 #include <KPushButton>
38 #include <KShortcutsDialog>
39 #include <KStandardAction>
40 #include <KStatusBar> // statusBar()
41 #include <KXMLGUIFactory>
42 #include <KActionCollection>
43 #include <KPluginLoader>
44 #include <KPluginFactory>
46 #include "ktimetrackerutility.h"
47 #include "ktimetracker.h"
48 #include "task.h"
49 #include "taskview.h"
50 #include "timekard.h"
51 #include "tray.h"
53 #include "timetrackerwidget.h"
55 MainWindow::MainWindow( const QString &icsfile )
56 : KParts::MainWindow( )
58 kDebug(5970) << "Entering function, icsfile is " << icsfile;
59 // Setup our actions
60 setupActions();
62 // this routine will find and load our Part.
63 KPluginLoader loader( "ktimetrackerpart" );
64 KPluginFactory *factory = loader.factory();
65 if (factory)
67 // now that the Part is loaded, we cast it to a Part to get our hands on it
69 //NOTE: Use the dynamic_cast below. Without it, KPluginLoader will use a qobject_cast
70 // that fails, because ktimetrackerpart is defined twice, once in ktimetracker's binary
71 // and another one in the plugin. The build system should be fixed.
72 //m_part = factory->create<ktimetrackerpart>( this );
74 m_part = dynamic_cast<ktimetrackerpart*>( factory->create<KParts::ReadWritePart>( this ) );
76 if (m_part)
78 // tell the KParts::MainWindow that this is indeed
79 // the main widget
80 setCentralWidget(m_part->widget());
81 m_part->openFile(icsfile);
82 slotSetCaption( icsfile ); // set the window title to our iCal file
83 connect(configureAction, SIGNAL(triggered(bool)),
84 m_part->widget(), SLOT(showSettingsDialog()));
85 ((TimetrackerWidget *) (m_part->widget()))->setupActions( actionCollection() );
86 setupGUI();
88 else
90 kError() << "Could not find the KTimeTracker part: m_part is 0";
91 KMessageBox::error(this, i18n( "Could not create the KTimeTracker part." ));
92 QTimer::singleShot(0, qApp, SLOT(quit()));
93 return;
96 else
98 // if we couldn't find our Part, we exit since the Shell by
99 // itself can't do anything useful
100 kError() << "Could not find the KTimeTracker part: factory is 0";
101 KMessageBox::error(this, i18n( "Could not find the KTimeTracker part." ));
102 QTimer::singleShot(0, qApp, SLOT(quit()));
103 // we return here, cause qApp->quit() only means "exit the
104 // next time we enter the event loop...
105 return;
107 setWindowFlags( windowFlags() | Qt::WindowContextHelpButtonHint );
109 // connections
110 connect( m_part->widget(), SIGNAL(statusBarTextChangeRequested(QString)),
111 this, SLOT(setStatusBar(QString)) );
112 connect( m_part->widget(), SIGNAL(setCaption(QString)),
113 this, SLOT(slotSetCaption(QString)) );
114 loadGeometry();
116 // Setup context menu request handling
117 connect( m_part->widget(),
118 SIGNAL(contextMenuRequested(QPoint)),
119 this,
120 SLOT(taskViewCustomContextMenuRequested(QPoint)) );
121 if (KTimeTrackerSettings::trayIcon())
123 _tray = new TrayIcon( this );
124 connect( m_part->widget(), SIGNAL(timersActive()), _tray, SLOT(startClock()) );
125 connect( m_part->widget(), SIGNAL(timersInactive()), _tray, SLOT(stopClock()) );
126 connect( m_part->widget(), SIGNAL(tasksChanged(QList<Task*>)), _tray, SLOT(updateToolTip(QList<Task*>)));
130 void MainWindow::setupActions()
132 configureAction = new KAction(this);
133 configureAction->setText(i18n("Configure KTimeTracker..."));
134 actionCollection()->addAction("configure_ktimetracker", configureAction);
137 void MainWindow::readProperties( const KConfigGroup &cfg )
139 if( cfg.readEntry( "WindowShown", true ))
140 show();
143 void MainWindow::saveProperties( KConfigGroup &cfg )
145 cfg.writeEntry( "WindowShown", isVisible());
148 void MainWindow::slotSetCaption( const QString& qs )
150 setCaption( qs );
153 void MainWindow::setStatusBar(const QString& qs)
155 statusBar()->showMessage(i18n(qs.toUtf8()));
158 MainWindow::~MainWindow()
160 kDebug(5970) << "MainWindow::~MainWindows: Quitting ktimetracker.";
161 saveGeometry();
164 void MainWindow::keyBindings()
166 KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
169 void MainWindow::makeMenus()
171 mainWidget->setupActions( actionCollection() );
172 actionKeyBindings = KStandardAction::keyBindings( this, SLOT(keyBindings()),
173 actionCollection() );
174 setupGUI();
175 actionKeyBindings->setToolTip( i18n( "Configure key bindings" ) );
176 actionKeyBindings->setWhatsThis( i18n( "This will let you configure key"
177 "bindings which are specific to ktimetracker" ) );
180 void MainWindow::loadGeometry()
182 if (initialGeometrySet()) setAutoSaveSettings();
183 else
185 KConfigGroup config = KGlobal::config()->group( QString::fromLatin1("Main Window Geometry") );
186 int w = config.readEntry( QString::fromLatin1("Width"), 100 );
187 int h = config.readEntry( QString::fromLatin1("Height"), 100 );
188 w = qMax( w, sizeHint().width() );
189 h = qMax( h, sizeHint().height() );
190 resize(w, h);
195 void MainWindow::saveGeometry()
197 KConfigGroup config = KGlobal::config()->group( QString::fromLatin1("Main Window Geometry") );
198 config.writeEntry( QString::fromLatin1("Width"), width());
199 config.writeEntry( QString::fromLatin1("Height"), height());
200 config.sync();
203 bool MainWindow::queryClose()
205 if ( !kapp->sessionSaving() )
207 hide();
208 return false;
210 return KMainWindow::queryClose();
213 void MainWindow::taskViewCustomContextMenuRequested( const QPoint& point )
215 QMenu* pop = dynamic_cast<QMenu*>( factory()->container( i18n( "task_popup" ), this ) );
216 if ( pop )
217 pop->popup( point );
220 #include "mainwindow.moc"