Don't create an AgentProgressMonitor instance every time this method is called; one...
[kdepim.git] / ktimetracker / mainwindow.cpp
blob6a8a469fcbb4063b07d436c3a4ff5c6b43e8cd8f
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>
29 #include <KAction>
30 #include <KApplication> // kapp
31 #include <KDebug>
32 #include <KGlobal>
33 #include <KIcon>
34 #include <KLocale> // i18n
35 #include <KMessageBox>
36 #include <KPushButton>
37 #include <KShortcutsDialog>
38 #include <KStandardAction>
39 #include <KStatusBar> // statusBar()
40 #include <KXMLGUIFactory>
41 #include <KActionCollection>
43 #include "ktimetrackerutility.h"
44 #include "ktimetracker.h"
45 #include "task.h"
46 #include "taskview.h"
47 #include "timekard.h"
48 #include "tray.h"
50 #include "timetrackerwidget.h"
52 MainWindow::MainWindow( const QString &icsfile )
53 : KParts::MainWindow( )
55 kDebug(5970) << "Entering function, icsfile is " << icsfile;
56 // Setup our actions
57 setupActions();
59 // this routine will find and load our Part.
60 KLibFactory *factory = KLibLoader::self()->factory("ktimetrackerpart");
61 if (factory)
63 // now that the Part is loaded, we cast it to a Part to get
64 // our hands on it
65 m_part = static_cast<ktimetrackerpart *>(factory->create(this, "ktimetrackerpart" ));
67 if (m_part)
69 // tell the KParts::MainWindow that this is indeed
70 // the main widget
71 setCentralWidget(m_part->widget());
72 m_part->openFile(icsfile);
73 slotSetCaption( icsfile ); // set the window title to our iCal file
74 connect(configureAction, SIGNAL(triggered(bool)),
75 m_part->widget(), SLOT(showSettingsDialog()));
76 ((TimetrackerWidget *) (m_part->widget()))->setupActions( actionCollection() );
77 setupGUI();
80 else
82 // if we couldn't find our Part, we exit since the Shell by
83 // itself can't do anything useful
84 KMessageBox::error(this, i18n( "Could not find the KTimeTracker part." ));
85 qApp->quit();
86 // we return here, cause qApp->quit() only means "exit the
87 // next time we enter the event loop...
88 return;
90 setWindowFlags( windowFlags() | Qt::WindowContextHelpButtonHint );
92 // connections
93 connect( m_part->widget(), SIGNAL( statusBarTextChangeRequested( QString ) ),
94 this, SLOT( setStatusBar( QString ) ) );
95 connect( m_part->widget(), SIGNAL( setCaption( const QString& ) ),
96 this, SLOT( slotSetCaption( const QString& ) ) );
97 loadGeometry();
99 // Setup context menu request handling
100 connect( m_part->widget(),
101 SIGNAL( contextMenuRequested( const QPoint& ) ),
102 this,
103 SLOT( taskViewCustomContextMenuRequested( const QPoint& ) ) );
105 _tray = new TrayIcon( this );
107 connect( _tray, SIGNAL( quitSelected() ), m_part->widget(), SLOT( quit() ) );
109 connect( m_part->widget(), SIGNAL( timersActive() ), _tray, SLOT( startClock() ) );
110 connect( m_part->widget(), SIGNAL( timersInactive() ), _tray, SLOT( stopClock() ) );
111 connect( m_part->widget(), SIGNAL( tasksChanged( const QList<Task*>& ) ),
112 _tray, SLOT( updateToolTip( QList<Task*> ) ));
115 void MainWindow::setupActions()
117 configureAction = new KAction(this);
118 configureAction->setText(i18n("Configure KTimeTracker..."));
119 actionCollection()->addAction("configure_ktimetracker", configureAction);
122 void MainWindow::readProperties( const KConfigGroup &cfg )
124 if( cfg.readEntry( "WindowShown", true ))
125 show();
128 void MainWindow::saveProperties( KConfigGroup &cfg )
130 cfg.writeEntry( "WindowShown", isVisible());
133 void MainWindow::slotSetCaption( const QString& qs )
135 setCaption( qs );
138 void MainWindow::setStatusBar(const QString& qs)
140 statusBar()->showMessage(i18n(qs.toUtf8()));
143 MainWindow::~MainWindow()
145 kDebug(5970) << "MainWindow::~MainWindows: Quitting ktimetracker.";
146 saveGeometry();
149 void MainWindow::keyBindings()
151 KShortcutsDialog::configure( actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this );
154 void MainWindow::makeMenus()
156 mainWidget->setupActions( actionCollection() );
157 actionKeyBindings = KStandardAction::keyBindings( this, SLOT( keyBindings() ),
158 actionCollection() );
159 setupGUI();
160 actionKeyBindings->setToolTip( i18n( "Configure key bindings" ) );
161 actionKeyBindings->setWhatsThis( i18n( "This will let you configure key"
162 "bindings which are specific to ktimetracker" ) );
165 void MainWindow::loadGeometry()
167 if (initialGeometrySet()) setAutoSaveSettings();
168 else
170 KConfigGroup config = KGlobal::config()->group( QString::fromLatin1("Main Window Geometry") );
171 int w = config.readEntry( QString::fromLatin1("Width"), 100 );
172 int h = config.readEntry( QString::fromLatin1("Height"), 100 );
173 w = qMax( w, sizeHint().width() );
174 h = qMax( h, sizeHint().height() );
175 resize(w, h);
180 void MainWindow::saveGeometry()
182 KConfigGroup config = KGlobal::config()->group( QString::fromLatin1("Main Window Geometry") );
183 config.writeEntry( QString::fromLatin1("Width"), width());
184 config.writeEntry( QString::fromLatin1("Height"), height());
185 config.sync();
188 bool MainWindow::queryClose()
190 if ( !kapp->sessionSaving() )
192 hide();
193 return false;
195 return KMainWindow::queryClose();
198 void MainWindow::taskViewCustomContextMenuRequested( const QPoint& point )
200 QMenu* pop = dynamic_cast<QMenu*>( factory()->container( i18n( "task_popup" ), this ) );
201 if ( pop )
202 pop->popup( point );
205 #include "mainwindow.moc"