source: branches/start/Taper/src/taper.cpp@ 9223

Last change on this file since 9223 was 4307, checked in by merck, 21 years ago
new project
File size: 8.8 KB
Line 
1/***************************************************************************
2 * Copyright (C) 2004 by Martin Merck *
3 * merck@astro.uni-wuerzburg.de *
4 * *
5 * This software is part of the MAGIC software. *
6 ***************************************************************************/
7
8#include "taper.h"
9#include "pref.h"
10
11#include <qdragobject.h>
12#include <kprinter.h>
13#include <qpainter.h>
14#include <qpaintdevicemetrics.h>
15
16#include <kglobal.h>
17#include <klocale.h>
18#include <kiconloader.h>
19#include <kdeversion.h>
20#include <kmenubar.h>
21#include <kstatusbar.h>
22#include <kkeydialog.h>
23#include <kaccel.h>
24#include <kio/netaccess.h>
25#include <kfiledialog.h>
26#include <kconfig.h>
27#include <kurl.h>
28#include <kurldrag.h>
29#include <kurlrequesterdlg.h>
30
31#include <kedittoolbar.h>
32
33#include <kstdaccel.h>
34#include <kaction.h>
35#include <kstdaction.h>
36
37Taper::Taper()
38 : KMainWindow( 0, "Taper" ),
39 m_view(new TaperView(this)),
40 m_printer(0)
41{
42 // accept dnd
43 setAcceptDrops(true);
44
45 // tell the KMainWindow that this is indeed the main widget
46 setCentralWidget(m_view);
47
48 // then, setup our actions
49 setupActions();
50
51 // and a status bar
52 statusBar()->show();
53
54 // apply the saved mainwindow settings, if any, and ask the mainwindow
55 // to automatically save settings if changed: window size, toolbar
56 // position, icon size, etc.
57 setAutoSaveSettings();
58
59 // allow the view to change the statusbar and caption
60 connect(m_view, SIGNAL(signalChangeStatusbar(const QString&)),
61 this, SLOT(changeStatusbar(const QString&)));
62 connect(m_view, SIGNAL(signalChangeCaption(const QString&)),
63 this, SLOT(changeCaption(const QString&)));
64
65}
66
67Taper::~Taper()
68{
69}
70
71void Taper::load(const KURL& url)
72{
73 QString target;
74 // the below code is what you should normally do. in this
75 // example case, we want the url to our own. you probably
76 // want to use this code instead for your app
77
78 #if 0
79 // download the contents
80 if (KIO::NetAccess::download(url, target))
81 {
82 // set our caption
83 setCaption(url);
84
85 // load in the file (target is always local)
86 loadFile(target);
87
88 // and remove the temp file
89 KIO::NetAccess::removeTempFile(target);
90 }
91 #endif
92
93 setCaption(url.prettyURL());
94 m_view->openURL(url);
95}
96
97void Taper::setupActions()
98{
99 KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
100 KStdAction::open(this, SLOT(fileOpen()), actionCollection());
101 KStdAction::save(this, SLOT(fileSave()), actionCollection());
102 KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
103 KStdAction::print(this, SLOT(filePrint()), actionCollection());
104 KStdAction::quit(kapp, SLOT(quit()), actionCollection());
105
106 m_toolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
107 m_statusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());
108
109 KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
110 KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
111 KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
112
113 // this doesn't do anything useful. it's just here to illustrate
114 // how to insert a custom menu and menu item
115 KAction *custom = new KAction(i18n("Cus&tom Menuitem"), 0,
116 this, SLOT(optionsPreferences()),
117 actionCollection(), "custom_action");
118 createGUI();
119}
120
121void Taper::saveProperties(KConfig *config)
122{
123 // the 'config' object points to the session managed
124 // config file. anything you write here will be available
125 // later when this app is restored
126
127 if (!m_view->currentURL().isEmpty()) {
128#if KDE_IS_VERSION(3,1,3)
129 config->writePathEntry("lastURL", m_view->currentURL());
130#else
131 config->writeEntry("lastURL", m_view->currentURL());
132#endif
133 }
134}
135
136void Taper::readProperties(KConfig *config)
137{
138 // the 'config' object points to the session managed
139 // config file. this function is automatically called whenever
140 // the app is being restored. read in here whatever you wrote
141 // in 'saveProperties'
142
143 QString url = config->readPathEntry("lastURL");
144
145 if (!url.isEmpty())
146 m_view->openURL(KURL(url));
147}
148
149void Taper::dragEnterEvent(QDragEnterEvent *event)
150{
151 // accept uri drops only
152 event->accept(KURLDrag::canDecode(event));
153}
154
155void Taper::dropEvent(QDropEvent *event)
156{
157 // this is a very simplistic implementation of a drop event. we
158 // will only accept a dropped URL. the Qt dnd code can do *much*
159 // much more, so please read the docs there
160 KURL::List urls;
161
162 // see if we can decode a URI.. if not, just ignore it
163 if (KURLDrag::decode(event, urls) && !urls.isEmpty())
164 {
165 // okay, we have a URI.. process it
166 const KURL &url = urls.first();
167
168 // load in the file
169 load(url);
170 }
171}
172
173void Taper::fileNew()
174{
175 // this slot is called whenever the File->New menu is selected,
176 // the New shortcut is pressed (usually CTRL+N) or the New toolbar
177 // button is clicked
178
179 // create a new window
180 (new Taper)->show();
181}
182
183void Taper::fileOpen()
184{
185 // this slot is called whenever the File->Open menu is selected,
186 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
187 // button is clicked
188/*
189 // this brings up the generic open dialog
190 KURL url = KURLRequesterDlg::getURL(QString::null, this, i18n("Open Location") );
191*/
192 // standard filedialog
193 KURL url = KFileDialog::getOpenURL(QString::null, QString::null, this, i18n("Open Location"));
194 if (!url.isEmpty())
195 m_view->openURL(url);
196}
197
198void Taper::fileSave()
199{
200 // this slot is called whenever the File->Save menu is selected,
201 // the Save shortcut is pressed (usually CTRL+S) or the Save toolbar
202 // button is clicked
203
204 // save the current file
205}
206
207void Taper::fileSaveAs()
208{
209 // this slot is called whenever the File->Save As menu is selected,
210 KURL file_url = KFileDialog::getSaveURL();
211 if (!file_url.isEmpty() && !file_url.isMalformed())
212 {
213 // save your info, here
214 }
215}
216
217void Taper::filePrint()
218{
219 // this slot is called whenever the File->Print menu is selected,
220 // the Print shortcut is pressed (usually CTRL+P) or the Print toolbar
221 // button is clicked
222 if (!m_printer) m_printer = new KPrinter;
223 if (m_printer->setup(this))
224 {
225 // setup the printer. with Qt, you always "print" to a
226 // QPainter.. whether the output medium is a pixmap, a screen,
227 // or paper
228 QPainter p;
229 p.begin(m_printer);
230
231 // we let our view do the actual printing
232 QPaintDeviceMetrics metrics(m_printer);
233 m_view->print(&p, metrics.height(), metrics.width());
234
235 // and send the result to the printer
236 p.end();
237 }
238}
239
240void Taper::optionsShowToolbar()
241{
242 // this is all very cut and paste code for showing/hiding the
243 // toolbar
244 if (m_toolbarAction->isChecked())
245 toolBar()->show();
246 else
247 toolBar()->hide();
248}
249
250void Taper::optionsShowStatusbar()
251{
252 // this is all very cut and paste code for showing/hiding the
253 // statusbar
254 if (m_statusbarAction->isChecked())
255 statusBar()->show();
256 else
257 statusBar()->hide();
258}
259
260void Taper::optionsConfigureKeys()
261{
262 KKeyDialog::configureKeys(actionCollection(), "taperui.rc");
263}
264
265void Taper::optionsConfigureToolbars()
266{
267 // use the standard toolbar editor
268#if defined(KDE_MAKE_VERSION)
269# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
270 saveMainWindowSettings(KGlobal::config(), autoSaveGroup());
271# else
272 saveMainWindowSettings(KGlobal::config());
273# endif
274#else
275 saveMainWindowSettings(KGlobal::config());
276#endif
277}
278
279void Taper::newToolbarConfig()
280{
281 // this slot is called when user clicks "Ok" or "Apply" in the toolbar editor.
282 // recreate our GUI, and re-apply the settings (e.g. "text under icons", etc.)
283 createGUI();
284
285#if defined(KDE_MAKE_VERSION)
286# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
287 applyMainWindowSettings(KGlobal::config(), autoSaveGroup());
288# else
289 applyMainWindowSettings(KGlobal::config());
290# endif
291#else
292 applyMainWindowSettings(KGlobal::config());
293#endif
294}
295
296void Taper::optionsPreferences()
297{
298 // popup some sort of preference dialog, here
299 TaperPreferences dlg;
300 if (dlg.exec())
301 {
302 // redo your settings
303 }
304}
305
306void Taper::changeStatusbar(const QString& text)
307{
308 // display the text on the statusbar
309 statusBar()->message(text);
310}
311
312void Taper::changeCaption(const QString& text)
313{
314 // display the text on the caption
315 setCaption(text);
316}
317#include "taper.moc"
Note: See TracBrowser for help on using the repository browser.