~ubuntu-branches/ubuntu/intrepid/gwenview/intrepid

« back to all changes in this revision

Viewing changes to gwenview/mainwindow.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher Martin
  • Date: 2005-04-06 11:33:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050406113306-7zovl7z0io5bacpd
Tags: 1.2.0-1
* New upstream release.
  + Fixes crashes when using "Back" to navigate. (Closes: #301811)
* Enable KIPI support.
* Add a doc-base file for the handbook.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Gwenview - A simple image viewer for KDE
3
 
Copyright (C) 2000-2002 Aur�lien G�teau
4
 
 
5
 
This program is free software; you can redistribute it and/or
6
 
modify it under the terms of the GNU General Public License
7
 
as published by the Free Software Foundation; either version 2
8
 
of the License, or (at your option) any later version.
9
 
 
10
 
This program is distributed in the hope that it will be useful,
11
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
GNU General Public License for more details.
14
 
 
15
 
You should have received a copy of the GNU General Public License
16
 
along with this program; if not, write to the Free Software
17
 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
 
19
 
*/
20
 
 
21
 
// KDE includes
22
 
#include <kaccel.h>
23
 
#include <kaction.h>
24
 
#include <kapp.h>
25
 
#include <kcmdlineargs.h>
26
 
#include <kconfig.h>
27
 
#include <kdebug.h>
28
 
#include <kfiledialog.h>
29
 
#include <kglobal.h>
30
 
#include <khelpmenu.h>
31
 
#include <kkeydialog.h>
32
 
#include <klocale.h>
33
 
#include <kmenubar.h>
34
 
#include <kmessagebox.h>
35
 
#include <kprogress.h>
36
 
#include <kstatusbar.h>
37
 
#include <kstdaccel.h>
38
 
#include <kstdaction.h>
39
 
 
40
 
// Our includes
41
 
#include "configdialog.h"
42
 
#include "dirview.h"
43
 
#include "fileview.h"
44
 
#include "fileoperation.h"
45
 
#include "gvpixmap.h"
46
 
#include "pixmapview.h"
47
 
#include "scrollpixmapview.h"
48
 
#include "statusbarprogress.h"
49
 
 
50
 
#include "mainwindow.moc"
51
 
 
52
 
 
53
 
enum { SB_FOLDER, SB_FILE};
54
 
 
55
 
const char* CONFIG_DOCK_GROUP="dock";
56
 
const char* CONFIG_MAINWINDOW_GROUP="main window";
57
 
const char* CONFIG_FILEWIDGET_GROUP="file widget";
58
 
const char* CONFIG_PIXMAPWIDGET_GROUP="pixmap widget";
59
 
const char* CONFIG_FILEOPERATION_GROUP="file operations";
60
 
 
61
 
 
62
 
MainWindow::MainWindow()
63
 
: KDockMainWindow(), mProgress(0L), mFullScreen(false)
64
 
{
65
 
        FileOperation::readConfig(KGlobal::config(),CONFIG_FILEOPERATION_GROUP);
66
 
 
67
 
// Backend
68
 
        mGVPixmap=new GVPixmap(this);
69
 
 
70
 
// GUI
71
 
        createWidgets();
72
 
        createActions();
73
 
        createAccels();
74
 
        createMenu();
75
 
        createFileViewPopupMenu();
76
 
        createScrollPixmapViewPopupMenu();
77
 
        createToolBar();
78
 
 
79
 
// Folder connections
80
 
        connect(mDirView,SIGNAL(folderChanged(QString)),
81
 
                this,SLOT(setPath(QString)) );
82
 
 
83
 
// Pixmap connections
84
 
        connect(mPixmapView,SIGNAL(selectPrevious()),
85
 
                mFileView,SLOT(slotSelectPrevious()) );
86
 
        connect(mPixmapView,SIGNAL(selectNext()),
87
 
                mFileView,SLOT(slotSelectNext()) );
88
 
        connect(mPixmapView,SIGNAL(escapePressed()),
89
 
                this,SLOT(escapePressed()) );
90
 
 
91
 
// Scroll pixmap connections
92
 
        connect(mPixmapView->scrollPixmapView(),SIGNAL(zoomChanged(double)),
93
 
                this,SLOT(updateFileStatusBar()) );
94
 
 
95
 
 
96
 
// Thumbnail view connections
97
 
        connect(mFileView,SIGNAL(updateStarted(int)),
98
 
                this,SLOT(thumbnailUpdateStarted(int)) );
99
 
        connect(mFileView,SIGNAL(updateEnded()),
100
 
                this,SLOT(thumbnailUpdateEnded()) );
101
 
        connect(mFileView,SIGNAL(updatedOneThumbnail()),
102
 
                this,SLOT(thumbnailUpdateProcessedOne()) );
103
 
 
104
 
// File view connections
105
 
        connect(mFileView,SIGNAL(filenameChanged(QString)),
106
 
                this,SLOT(setFilename(QString)) );
107
 
        connect(mFileView,SIGNAL(completed()),
108
 
                this,SLOT(updateStatusBar()) );
109
 
        connect(mFileView,SIGNAL(canceled()),
110
 
                this,SLOT(updateStatusBar()) );
111
 
                
112
 
// GVPixmap connections
113
 
        connect(mGVPixmap,SIGNAL(loading()),
114
 
                this,SLOT(pixmapLoading()) );
115
 
        connect(mGVPixmap,SIGNAL(loaded()),
116
 
                this,SLOT(pixmapLoaded()) );
117
 
 
118
 
 
119
 
// Command line
120
 
        KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
121
 
 
122
 
        QString fullPath;
123
 
        if (args->count()==0) {
124
 
                fullPath=QDir::currentDirPath();
125
 
        } else {
126
 
                fullPath=QFile::decodeName(args->arg(0));
127
 
        }
128
 
 
129
 
// Go to requested file
130
 
        setPath(fullPath);
131
 
}
132
 
 
133
 
 
134
 
MainWindow::~MainWindow() {
135
 
        KConfig* config=KGlobal::config();
136
 
        FileOperation::writeConfig(config,CONFIG_FILEOPERATION_GROUP);
137
 
        mPixmapView->writeConfig(config,CONFIG_PIXMAPWIDGET_GROUP);
138
 
        mFileView->writeConfig(config,CONFIG_FILEWIDGET_GROUP);
139
 
        writeDockConfig(config,CONFIG_DOCK_GROUP);
140
 
        mAccel->writeSettings();
141
 
}
142
 
 
143
 
 
144
 
//-Public slots----------------------------------------------------------
145
 
void MainWindow::setPath(QString fullPath) {
146
 
        QFileInfo fileInfo(fullPath);
147
 
    bool filenameIsValid;
148
 
 
149
 
// Init mFolder and mFilename
150
 
        if (fileInfo.isFile()) {
151
 
                mFolder=fileInfo.dirPath(true);
152
 
                mFilename=fileInfo.fileName();
153
 
                filenameIsValid=true;
154
 
                mGVPixmap->load(fullPath);
155
 
        } else {
156
 
                filenameIsValid=false;
157
 
                mFolder=fileInfo.absFilePath();
158
 
                mFilename="";
159
 
        }
160
 
 
161
 
// Update actions
162
 
        mToggleFullScreen->setEnabled(filenameIsValid);
163
 
        mRenameFile->setEnabled(filenameIsValid);
164
 
        mCopyFile->setEnabled(filenameIsValid);
165
 
        mMoveFile->setEnabled(filenameIsValid);
166
 
        mDeleteFile->setEnabled(filenameIsValid);
167
 
 
168
 
// Update widgets
169
 
        mFileView->setFolderAndFile(mFolder,mFilename);
170
 
        mDirView->setFolder(mFolder);
171
 
        
172
 
        if (filenameIsValid) {
173
 
                mGVPixmap->load(fullPath);
174
 
        } else {
175
 
                mGVPixmap->reset();
176
 
        }
177
 
}
178
 
 
179
 
 
180
 
void MainWindow::setFilename(QString filename) {
181
 
        if (mFilename==filename) return;
182
 
 
183
 
        mFilename=filename;
184
 
 
185
 
        bool filenameIsValid= mFilename!="";
186
 
 
187
 
        mToggleFullScreen->setEnabled(filenameIsValid);
188
 
        mRenameFile->setEnabled(filenameIsValid);
189
 
        mCopyFile->setEnabled(filenameIsValid);
190
 
        mMoveFile->setEnabled(filenameIsValid);
191
 
        mDeleteFile->setEnabled(filenameIsValid);
192
 
        
193
 
        if (filenameIsValid) {
194
 
                mFileView->selectFilename(mFilename);
195
 
                mGVPixmap->load(mFolder + '/' + mFilename);
196
 
        } else {
197
 
                mGVPixmap->reset();
198
 
        }
199
 
        updateFileStatusBar();
200
 
}
201
 
 
202
 
 
203
 
//-Private slots---------------------------------------------------------
204
 
void MainWindow::pixmapLoading() {
205
 
        kapp->setOverrideCursor(QCursor(WaitCursor));
206
 
}
207
 
 
208
 
 
209
 
void MainWindow::pixmapLoaded() {
210
 
        kapp->restoreOverrideCursor();
211
 
        if ( (!mFilename.isEmpty()) && mGVPixmap->isNull()) {
212
 
                KMessageBox::sorry(this,i18n("Could not load <b>%1</b>").arg(mFilename));
213
 
        }
214
 
}
215
 
 
216
 
 
217
 
void MainWindow::toggleFullScreen() {
218
 
        KConfig* config=KGlobal::config();
219
 
 
220
 
        if (!mFullScreen) {
221
 
                mFullScreen=true;
222
 
                menuBar()->hide();
223
 
                toolBar()->hide();
224
 
                statusBar()->hide();
225
 
                writeDockConfig(config,CONFIG_DOCK_GROUP);
226
 
                makeDockInvisible(mFileDock);
227
 
                makeDockInvisible(mFolderDock);
228
 
                showFullScreen();
229
 
 
230
 
                mPixmapView->setFullScreen(true);
231
 
        } else {
232
 
                mFullScreen=false;
233
 
                readDockConfig(config,CONFIG_DOCK_GROUP);
234
 
                statusBar()->show();
235
 
                toolBar()->show();
236
 
                menuBar()->show();
237
 
                showNormal();
238
 
 
239
 
                mPixmapView->setFullScreen(false);
240
 
        }
241
 
}
242
 
 
243
 
 
244
 
void MainWindow::showConfigDialog() {
245
 
        ConfigDialog dialog(this,this);
246
 
        dialog.exec();
247
 
}
248
 
 
249
 
 
250
 
void MainWindow::showKeyDialog() {
251
 
        KKeyDialog::configureKeys(mAccel);
252
 
}
253
 
 
254
 
 
255
 
void MainWindow::escapePressed() {
256
 
        if (mFullScreen) {
257
 
                mToggleFullScreen->activate();
258
 
        }
259
 
}
260
 
 
261
 
 
262
 
void MainWindow::openFile() {
263
 
        QString path=KFileDialog::getOpenFileName();
264
 
        if (path.isNull()) return;
265
 
        setPath(path);
266
 
}
267
 
 
268
 
void MainWindow::openWithEditor() {
269
 
        if (mFilename.isEmpty()) return;
270
 
 
271
 
        FileOperation::openWithEditor(mFolder + '/' + mFilename);
272
 
}
273
 
 
274
 
 
275
 
void MainWindow::thumbnailUpdateStarted(int count) {
276
 
        mProgress=new StatusBarProgress(statusBar(),i18n("Generating thumbnails..."),count);
277
 
        mProgress->progress()->setFormat("%v/%m");
278
 
        mProgress->show();
279
 
        mStop->setEnabled(true);
280
 
}
281
 
 
282
 
 
283
 
void MainWindow::thumbnailUpdateEnded() {
284
 
        mStop->setEnabled(false);
285
 
        if (mProgress) {
286
 
                mProgress->hide();
287
 
                delete mProgress;
288
 
                mProgress=0L;
289
 
        }
290
 
}
291
 
 
292
 
 
293
 
void MainWindow::thumbnailUpdateProcessedOne() {
294
 
        mProgress->progress()->advance(1);
295
 
}
296
 
 
297
 
 
298
 
 
299
 
//-GUI-------------------------------------------------------------------
300
 
void MainWindow::updateStatusBar() {
301
 
        QString txt;
302
 
        uint count=mFileView->fileCount();
303
 
        if (count>1) {
304
 
                txt=i18n("%1 - %2 images");
305
 
        } else {
306
 
                txt=i18n("%1 - %2 image");
307
 
        }
308
 
        txt=txt.arg(mFolder).arg(count);
309
 
 
310
 
        statusBar()->changeItem( txt, SB_FOLDER );
311
 
        updateFileStatusBar();
312
 
}
313
 
 
314
 
 
315
 
void MainWindow::updateFileStatusBar() {
316
 
        QString txt;
317
 
        if (!mFilename.isEmpty()) {
318
 
                txt=QString("%1 %2x%3 @ %4%")
319
 
                        .arg(mFilename).arg(mGVPixmap->width()).arg(mGVPixmap->height())
320
 
                        .arg(int(mPixmapView->scrollPixmapView()->zoom()*100) );
321
 
        } else {
322
 
                txt="";
323
 
        }
324
 
        statusBar()->changeItem( txt, SB_FILE );
325
 
}
326
 
 
327
 
 
328
 
void MainWindow::createWidgets() {
329
 
        KConfig* config=KGlobal::config();
330
 
 
331
 
// Status bar
332
 
        statusBar()->insertItem("",SB_FOLDER);
333
 
        statusBar()->setItemAlignment(SB_FOLDER, AlignLeft|AlignVCenter);
334
 
        statusBar()->insertItem("",SB_FILE,1);
335
 
        statusBar()->setItemAlignment(SB_FILE, AlignLeft|AlignVCenter);
336
 
 
337
 
// Pixmap widgets
338
 
        mPixmapDock = createDockWidget("Image",SmallIcon("gwenview"),NULL,i18n("Image"));
339
 
 
340
 
        mPixmapView=new PixmapView(mPixmapDock,mGVPixmap);
341
 
        mPixmapDock->setWidget(mPixmapView);
342
 
        setView(mPixmapDock);
343
 
        setMainDockWidget(mPixmapDock);
344
 
 
345
 
// Folder widget
346
 
        mFolderDock = createDockWidget("Folders",SmallIcon("folder_open"),NULL,i18n("Folders"));
347
 
        mDirView=new DirView(mFolderDock);
348
 
        mFolderDock->setWidget(mDirView);
349
 
 
350
 
// File widget
351
 
        mFileDock = createDockWidget("Files",SmallIcon("image"),NULL,i18n("Files"));
352
 
        mFileView=new FileView(this);
353
 
        mFileDock->setWidget(mFileView);
354
 
 
355
 
// Default dock config
356
 
        setGeometry(20,20,600,400);
357
 
        mFolderDock->manualDock( mPixmapDock,KDockWidget::DockLeft,30);
358
 
        mFileDock->manualDock( mPixmapDock,KDockWidget::DockTop,30);
359
 
 
360
 
// Load config
361
 
        readDockConfig(config,CONFIG_DOCK_GROUP);
362
 
        mFileView->readConfig(config,CONFIG_FILEWIDGET_GROUP);
363
 
        mPixmapView->readConfig(config,CONFIG_PIXMAPWIDGET_GROUP);
364
 
}
365
 
 
366
 
 
367
 
void MainWindow::createActions() {
368
 
        mOpenFile=KStdAction::open(this, SLOT(openFile()),actionCollection() );
369
 
 
370
 
        mRenameFile=new KAction(i18n("&Rename..."),Key_F2,mFileView,SLOT(renameFile()),actionCollection(),"file_new");
371
 
 
372
 
        mCopyFile=new KAction(i18n("&Copy to..."),Key_F5,mFileView,SLOT(copyFile()),actionCollection(),"file_copy");
373
 
 
374
 
        mMoveFile=new KAction(i18n("&Move to..."),Key_F6,mFileView,SLOT(moveFile()),actionCollection(),"file_move");
375
 
 
376
 
        mDeleteFile=new KAction(i18n("&Delete"),"editdelete",Key_Delete,mFileView,SLOT(deleteFile()),actionCollection(),"file_delete");
377
 
 
378
 
        mOpenWithEditor=new KAction(i18n("&Open with editor"),"paintbrush",0,this,SLOT(openWithEditor()),actionCollection(),"file_edit");
379
 
 
380
 
        mToggleFullScreen=new KAction(i18n("Full screen"),"window_fullscreen",CTRL + Key_F,this,SLOT(toggleFullScreen()),actionCollection(),"view_fullscreen");
381
 
 
382
 
        mShowConfigDialog=new KAction(i18n("Configure Gwenview"),"configure",0,this,SLOT(showConfigDialog()),actionCollection());
383
 
 
384
 
        mShowKeyDialog=KStdAction::keyBindings(this,SLOT(showKeyDialog()),actionCollection());
385
 
 
386
 
        mStop=new KAction(i18n("Stop"),"stop",Key_Escape,mFileView,SLOT(cancel()),actionCollection());
387
 
        mStop->setEnabled(false);
388
 
}
389
 
 
390
 
 
391
 
void MainWindow::createAccels() {
392
 
// Associate actions with accelerator
393
 
        mAccel=new KAccel(this);
394
 
        int count=actionCollection()->count();
395
 
 
396
 
        for (int pos=0;pos<count;++pos) {
397
 
                KAction *action = actionCollection()->action(pos);
398
 
                if ( action->accel() ) {
399
 
                        action->plugAccel(mAccel);
400
 
                }
401
 
        }
402
 
        mFileView->plugActionsToAccel(mAccel);
403
 
        mPixmapView->scrollPixmapView()->plugActionsToAccel(mAccel);
404
 
 
405
 
// Read user accelerator
406
 
        mAccel->readSettings();
407
 
}
408
 
 
409
 
 
410
 
void MainWindow::createMenu() {
411
 
        QPopupMenu* fileMenu = new QPopupMenu;
412
 
 
413
 
        mOpenFile->plug(fileMenu);
414
 
        fileMenu->insertSeparator();
415
 
        mOpenWithEditor->plug(fileMenu);
416
 
        mRenameFile->plug(fileMenu);
417
 
        mCopyFile->plug(fileMenu);
418
 
        mMoveFile->plug(fileMenu);
419
 
        mDeleteFile->plug(fileMenu);
420
 
        fileMenu->insertSeparator();
421
 
        KStdAction::quit( kapp, SLOT (closeAllWindows()) )->plug(fileMenu);
422
 
        menuBar()->insertItem(i18n("&File"), fileMenu);
423
 
 
424
 
        QPopupMenu* viewMenu = new QPopupMenu;
425
 
        mStop->plug(viewMenu);
426
 
        
427
 
        viewMenu->insertSeparator();
428
 
        mFileView->noThumbnails()->plug(viewMenu);
429
 
        mFileView->smallThumbnails()->plug(viewMenu);
430
 
        mFileView->medThumbnails()->plug(viewMenu);
431
 
        mFileView->largeThumbnails()->plug(viewMenu);
432
 
        
433
 
        viewMenu->insertSeparator();
434
 
        mToggleFullScreen->plug(viewMenu);
435
 
        mPixmapView->scrollPixmapView()->zoomIn()->plug(viewMenu);
436
 
        mPixmapView->scrollPixmapView()->zoomOut()->plug(viewMenu);
437
 
        mPixmapView->scrollPixmapView()->resetZoom()->plug(viewMenu);
438
 
        mPixmapView->scrollPixmapView()->autoZoom()->plug(viewMenu);
439
 
        menuBar()->insertItem(i18n("&View"), viewMenu);
440
 
 
441
 
        QPopupMenu* goMenu = new QPopupMenu;
442
 
        mFileView->selectFirst()->plug(goMenu);
443
 
        mFileView->selectPrevious()->plug(goMenu);
444
 
        mFileView->selectNext()->plug(goMenu);
445
 
        mFileView->selectLast()->plug(goMenu);
446
 
        menuBar()->insertItem(i18n("&Go"), goMenu);
447
 
 
448
 
        QPopupMenu* settingsMenu = new QPopupMenu;
449
 
        mShowConfigDialog->plug(settingsMenu);
450
 
        mShowKeyDialog->plug(settingsMenu);
451
 
        menuBar()->insertItem(i18n("&Settings"),settingsMenu);
452
 
 
453
 
        menuBar()->insertItem(i18n("&Windows"), dockHideShowMenu());
454
 
 
455
 
        menuBar()->insertItem(i18n("&Help"), helpMenu());
456
 
 
457
 
        menuBar()->show();
458
 
}
459
 
 
460
 
 
461
 
void MainWindow::createFileViewPopupMenu() {
462
 
        QPopupMenu* menu=new QPopupMenu(this);
463
 
 
464
 
        mOpenWithEditor->plug(menu);
465
 
        mRenameFile->plug(menu);
466
 
        mCopyFile->plug(menu);
467
 
        mMoveFile->plug(menu);
468
 
        mDeleteFile->plug(menu);
469
 
 
470
 
        mFileView->installRBPopup(menu);
471
 
}
472
 
 
473
 
 
474
 
void MainWindow::createScrollPixmapViewPopupMenu() {
475
 
        QPopupMenu* menu=new QPopupMenu(this);
476
 
 
477
 
        mPixmapView->scrollPixmapView()->zoomIn()->plug(menu);
478
 
        mPixmapView->scrollPixmapView()->zoomOut()->plug(menu);
479
 
        mPixmapView->scrollPixmapView()->resetZoom()->plug(menu);
480
 
        mPixmapView->scrollPixmapView()->autoZoom()->plug(menu);
481
 
 
482
 
        menu->insertSeparator();
483
 
        mToggleFullScreen->plug(menu);
484
 
 
485
 
        menu->insertSeparator();
486
 
        mFileView->selectFirst()->plug(menu);
487
 
        mFileView->selectPrevious()->plug(menu);
488
 
        mFileView->selectNext()->plug(menu);
489
 
        mFileView->selectLast()->plug(menu);
490
 
 
491
 
        menu->insertSeparator();
492
 
        mOpenWithEditor->plug(menu);
493
 
        mRenameFile->plug(menu);
494
 
        mCopyFile->plug(menu);
495
 
        mMoveFile->plug(menu);
496
 
        mDeleteFile->plug(menu);
497
 
 
498
 
        mPixmapView->scrollPixmapView()->installRBPopup(menu);
499
 
}
500
 
 
501
 
 
502
 
 
503
 
void MainWindow::createToolBar() {
504
 
        mFileView->selectFirst()->plug(toolBar());
505
 
        mFileView->selectPrevious()->plug(toolBar());
506
 
        mFileView->selectNext()->plug(toolBar());
507
 
        mFileView->selectLast()->plug(toolBar());
508
 
        toolBar()->insertLineSeparator();
509
 
 
510
 
        mStop->plug(toolBar());
511
 
        toolBar()->insertLineSeparator();
512
 
        mFileView->noThumbnails()->plug(toolBar());
513
 
        mFileView->smallThumbnails()->plug(toolBar());
514
 
        mFileView->medThumbnails()->plug(toolBar());
515
 
        mFileView->largeThumbnails()->plug(toolBar());
516
 
        
517
 
        toolBar()->insertLineSeparator();
518
 
        mToggleFullScreen->plug(toolBar());
519
 
        mPixmapView->scrollPixmapView()->zoomIn()->plug(toolBar());
520
 
        mPixmapView->scrollPixmapView()->zoomOut()->plug(toolBar());
521
 
        mPixmapView->scrollPixmapView()->resetZoom()->plug(toolBar());
522
 
        mPixmapView->scrollPixmapView()->autoZoom()->plug(toolBar());
523
 
        toolBar()->show();
524
 
}
525