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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
// vim: set tabstop=4 shiftwidth=4 noexpandtab
/*
Gwenview - A simple image viewer for TDE
Copyright 2006 Aurélien Gâteau
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <imageviewcontroller.moc>
// TQt
#include <tqcursor.h>
#include <tqlayout.h>
#include <tqpopupmenu.h>
#include <tqvbox.h>
#include <tqwidgetstack.h>
// KDE
#include <tdeaction.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <tdemediaplayer/player.h>
#include <kmimetype.h>
#include <tdetoolbar.h>
#include <kuserprofile.h>
#include <tdeparts/componentfactory.h>
#include <kxmlguibuilder.h>
#include <kxmlguifactory.h>
// Local
#include <document.h>
#include <externaltoolcontext.h>
#include <externaltoolmanager.h>
#include <fullscreenbar.h>
#include <imageview.h>
namespace Gwenview {
#undef ENABLE_LOG
#undef LOG
//#define ENABLE_LOG
#ifdef ENABLE_LOG
#define LOG(x) kdDebug() << k_funcinfo << x << endl
#else
#define LOG(x) ;
#endif
/**
* A KXMLGUIBuilder which only creates containers for toolbars.
*/
class XMLGUIBuilder : public KXMLGUIBuilder {
public:
XMLGUIBuilder(TQWidget* parent) : KXMLGUIBuilder(parent) {}
virtual TQWidget* createContainer(TQWidget *parent, int index, const TQDomElement &element, int &id) {
if (element.tagName().lower() == "toolbar") {
return KXMLGUIBuilder::createContainer(parent, index, element, id);
} else {
return 0;
}
}
};
const int AUTO_HIDE_TIMEOUT=4000;
//------------------------------------------------------------------------
//
// ImageViewController::Private
//
//------------------------------------------------------------------------
struct ImageViewController::Private {
ImageViewController* mImageViewController;
Document* mDocument;
TDEActionCollection* mActionCollection;
TQWidget* mContainer;
TDEToolBar* mToolBar;
KXMLGUIFactory* mFactory;
XMLGUIBuilder* mBuilder;
TQWidgetStack* mStack;
ImageView* mImageView;
TDEActionPtrList mImageViewActions;
// Hide cursor stuff
TQTimer* mAutoHideTimer;
bool mCursorHidden;
KParts::ReadOnlyPart* mPlayerPart;
// Fullscreen stuff
bool mFullScreen;
FullScreenBar* mFullScreenBar;
TDEActionPtrList mFullScreenCommonActions;
void setXMLGUIClient(KXMLGUIClient* client) {
TQPtrList<KXMLGUIClient> list=mFactory->clients();
KXMLGUIClient* oldClient=list.getFirst();
if (oldClient) {
mFactory->removeClient(oldClient);
// There should be at most one client, so the list should be empty
// now
Q_ASSERT(!mFactory->clients().getFirst());
}
// Unplug image view actions, if plugged
TDEActionPtrList::Iterator
it=mImageViewActions.begin(),
end=mImageViewActions.end();
for (; it!=end; ++it) {
TDEAction* action=*it;
if (action->isPlugged(mToolBar)) {
action->unplug(mToolBar);
}
}
if (client) {
mFactory->addClient(client);
}
}
void createPlayerPart(void) {
if (mPlayerPart) {
setXMLGUIClient(0);
delete mPlayerPart;
}
mPlayerPart=0;
TQString mimeType=KMimeType::findByURL(mDocument->url())->name();
KService::Ptr service = KServiceTypeProfile::preferredService(mimeType, "KParts/ReadOnlyPart");
if (!service) {
kdWarning() << "Couldn't find a KPart for " << mimeType << endl;
return;
}
TQString library=service->library();
Q_ASSERT(!library.isNull());
LOG("Library:" << library);
mPlayerPart = KParts::ComponentFactory::createPartInstanceFromService<KParts::ReadOnlyPart>(service, TQT_TQWIDGET(mStack), 0, TQT_TQOBJECT(mStack), 0);
if (!mPlayerPart) {
kdWarning() << "Failed to instantiate KPart from library " << library << endl;
return;
}
mStack->addWidget(mPlayerPart->widget());
setXMLGUIClient(mPlayerPart);
}
void showPlayerPart(void) {
LOG("");
createPlayerPart();
if (!mPlayerPart) return;
mStack->raiseWidget(mPlayerPart->widget());
mPlayerPart->openURL(mDocument->url());
// If the part implements the KMediaPlayer::Player interface, start
// playing (needed for Kaboodle)
KMediaPlayer::Player* player=dynamic_cast<KMediaPlayer::Player *>(mPlayerPart);
if (player) {
player->play();
}
}
void showImageView(void) {
LOG("");
if (mStack->visibleWidget()==mImageView) {
TDEAction* action=mImageViewActions.first();
if (action && !action->isPlugged(mToolBar)) {
// In the ctor, we set the imageview as the visible widget but
// we did not fill the toolbar because mImageViewActions was
// empty. In this case, fill the toolbar now.
plugImageViewActions();
}
return;
}
if (mPlayerPart) {
setXMLGUIClient(0);
delete mPlayerPart;
mPlayerPart=0;
}
plugImageViewActions();
mStack->raiseWidget(mImageView);
}
void plugImageViewActions() {
TDEActionPtrList::Iterator
it=mImageViewActions.begin(),
end=mImageViewActions.end();
for (; it!=end; ++it) {
TDEAction* action=*it;
action->plug(mToolBar);
}
}
void restartAutoHideTimer() {
mAutoHideTimer->start(AUTO_HIDE_TIMEOUT,true);
}
void updateFullScreenBarPosition() {
int mouseY=mStack->mapFromGlobal(TQCursor::pos()).y();
bool visible = mFullScreenBar->y()==0;
if (visible && mouseY>mFullScreenBar->height()) {
mFullScreenBar->slideOut();
}
if (!visible && mouseY<2) {
mFullScreenBar->slideIn();
}
}
/**
* This function creates the fullscreen toolbar.
* NOTE: It should not be called from/merged with setFullScreenActions,
* otherwise the toolbar will have a one pixel border which will prevent
* reaching easily buttons by pushing the mouse to the top edge of the
* screen.
* My guess is that instanciating the toolbar *before* the main
* window is shown causes the main window to tweak its bars. This happens
* with KDE 3.5.1.
*/
void initFullScreenBar() {
Q_ASSERT(!mFullScreenBar);
mFullScreenBar=new FullScreenBar(mContainer);
TDEActionPtrList::ConstIterator
it=mFullScreenCommonActions.begin(),
end=mFullScreenCommonActions.end();
for (; it!=end; ++it) {
(*it)->plug(mFullScreenBar);
}
}
};
//------------------------------------------------------------------------
//
// ImageViewController
//
//------------------------------------------------------------------------
ImageViewController::ImageViewController(TQWidget* parent, Document* document, TDEActionCollection* actionCollection)
: TQObject(parent) {
d=new Private;
d->mImageViewController=this;
d->mDocument=document;
d->mActionCollection=actionCollection;
d->mAutoHideTimer=new TQTimer(this);
d->mCursorHidden=false;
d->mContainer=new TQWidget(parent);
d->mContainer->setMinimumWidth(1); // Make sure we can resize the toolbar smaller than its minimum size
TQVBoxLayout* layout=new TQVBoxLayout(d->mContainer);
d->mToolBar=new TDEToolBar(d->mContainer, "", true);
layout->add(d->mToolBar);
d->mStack=new TQWidgetStack(d->mContainer);
layout->add(d->mStack);
d->mImageView=new ImageView(d->mStack, document, actionCollection);
d->mStack->addWidget(d->mImageView);
TDEApplication::kApplication()->installEventFilter(this);
d->mPlayerPart=0;
d->mBuilder=new XMLGUIBuilder(d->mToolBar);
d->mFactory=new KXMLGUIFactory(d->mBuilder, this);
d->mFullScreen=false;
d->mFullScreenBar=0;
connect(d->mDocument,TQT_SIGNAL(loaded(const KURL&)),
this,TQT_SLOT(slotLoaded()) );
connect(d->mImageView, TQT_SIGNAL(requestContextMenu(const TQPoint&)),
this, TQT_SLOT(openImageViewContextMenu(const TQPoint&)) );
connect(d->mImageView, TQT_SIGNAL(requestHintDisplay(const TQString&)),
this, TQT_SIGNAL(requestHintDisplay(const TQString&)) );
connect(d->mAutoHideTimer,TQT_SIGNAL(timeout()),
this,TQT_SLOT(slotAutoHide()) );
// Forward Image view signals
connect(d->mImageView, TQT_SIGNAL(selectPrevious()), TQT_SIGNAL(selectPrevious()) );
connect(d->mImageView, TQT_SIGNAL(selectNext()), TQT_SIGNAL(selectNext()) );
connect(d->mImageView, TQT_SIGNAL(doubleClicked()), TQT_SIGNAL(doubleClicked()) );
}
ImageViewController::~ImageViewController() {
delete d->mBuilder;
delete d;
}
void ImageViewController::setFocus() {
TQWidget* view;
if (d->mPlayerPart) {
view = d->mPlayerPart->widget();
} else {
view = d->mImageView;
}
view->setFocus();
}
void ImageViewController::slotLoaded() {
LOG("");
if (d->mDocument->urlKind()==MimeTypeUtils::KIND_FILE) {
d->showPlayerPart();
} else {
d->showImageView();
}
}
void ImageViewController::setFullScreen(bool fullScreen) {
d->mFullScreen=fullScreen;
d->mImageView->setFullScreen(fullScreen);
if (d->mFullScreen) {
d->restartAutoHideTimer();
if (!d->mFullScreenBar) {
d->initFullScreenBar();
}
} else {
d->mAutoHideTimer->stop();
TQApplication::restoreOverrideCursor();
d->mCursorHidden=false;
}
d->mToolBar->setHidden(d->mFullScreen);
if (d->mFullScreenBar) {
d->mFullScreenBar->setHidden(!d->mFullScreen);
}
}
void ImageViewController::setNormalCommonActions(const TDEActionPtrList& actions) {
TDEActionPtrList::ConstIterator
it=actions.begin(),
end=actions.end();
for (; it!=end; ++it) {
(*it)->plug(d->mToolBar);
}
d->mToolBar->insertLineSeparator();
}
void ImageViewController::setFullScreenCommonActions(const TDEActionPtrList& actions) {
d->mFullScreenCommonActions=actions;
}
void ImageViewController::setImageViewActions(const TDEActionPtrList& actions) {
d->mImageViewActions=actions;
}
void ImageViewController::slotAutoHide() {
if (d->mFullScreenBar) {
// Do not auto hide if the cursor is over the bar
TQPoint pos=d->mFullScreenBar->mapFromGlobal(TQCursor::pos());
if (TQT_TQRECT_OBJECT(d->mFullScreenBar->rect()).contains(pos)) {
d->restartAutoHideTimer();
return;
}
}
// Only hide cursor if we are not over a dialog
TQWidget* widget = TQT_TQWIDGET(TDEApplication::kApplication()->activeWindow());
if (!widget || !widget->inherits(TQDIALOG_OBJECT_NAME_STRING)) {
TQApplication::setOverrideCursor(blankCursor);
d->mCursorHidden=true;
}
}
TQWidget* ImageViewController::widget() const {
return d->mContainer;
}
void ImageViewController::updateFromSettings() {
d->mImageView->updateFromSettings();
}
/**
* This event filter monitors mouse moves and make sure the position of the
* fullscreen bar is updated.
*/
bool ImageViewController::eventFilter(TQObject* object, TQEvent* event) {
if (!d->mFullScreen) return false;
if (!event->type()==TQEvent::MouseMove) return false;
// Check we must filter this object. This is an application filter, so we
// have to check we are not dealing with another object.
bool isAChildOfStack=false;
TQObject* parentObject;
for (parentObject=object->parent(); parentObject; parentObject=parentObject->parent()) {
if (TQT_BASE_OBJECT(parentObject)==TQT_BASE_OBJECT(d->mStack)) {
isAChildOfStack=true;
break;
}
}
if (!isAChildOfStack) return false;
d->updateFullScreenBarPosition();
if (event->type()==TQEvent::MouseMove) {
d->mCursorHidden=false;
d->restartAutoHideTimer();
}
if (d->mCursorHidden) {
TQApplication::setOverrideCursor(blankCursor,true);
} else {
TQApplication::restoreOverrideCursor();
}
return false;
}
/**
* Little helper to plug an action if it exists
*/
inline void plugAction(TQPopupMenu* menu, TDEActionCollection* actionCollection, const char* actionName) {
TDEAction* action=actionCollection->action(actionName);
if (action) action->plug(menu);
}
void ImageViewController::openImageViewContextMenu(const TQPoint& pos) {
TQPopupMenu menu(d->mImageView);
bool noImage=d->mDocument->filename().isEmpty();
bool validImage=!d->mDocument->isNull();
// The fullscreen item is always there, to be able to leave fullscreen mode
// if necessary. But KParts may not have the action itself.
plugAction(&menu, d->mActionCollection, "fullscreen");
plugAction(&menu, d->mActionCollection, "slideshow");
if (validImage) {
menu.insertSeparator();
plugAction(&menu, d->mActionCollection, "view_zoom_to_fit");
plugAction(&menu, d->mActionCollection, "view_zoom_in");
plugAction(&menu, d->mActionCollection, "view_zoom_out");
plugAction(&menu, d->mActionCollection, "view_actual_size");
plugAction(&menu, d->mActionCollection, "view_zoom_lock");
}
menu.insertSeparator();
plugAction(&menu, d->mActionCollection, "first");
plugAction(&menu, d->mActionCollection, "previous");
plugAction(&menu, d->mActionCollection, "next");
plugAction(&menu, d->mActionCollection, "last");
if (validImage) {
menu.insertSeparator();
TQPopupMenu* editMenu=new TQPopupMenu(&menu);
plugAction(editMenu, d->mActionCollection, "rotate_left");
plugAction(editMenu, d->mActionCollection, "rotate_right");
plugAction(editMenu, d->mActionCollection, "mirror");
plugAction(editMenu, d->mActionCollection, "flip");
plugAction(editMenu, d->mActionCollection, "adjust_bcg");
menu.insertItem( i18n("Edit"), editMenu );
ExternalToolContext* externalToolContext=
ExternalToolManager::instance()->createContext(
this, d->mDocument->url());
menu.insertItem(
i18n("External Tools"), externalToolContext->popupMenu());
}
if (!noImage) {
menu.insertSeparator();
plugAction(&menu, d->mActionCollection, "file_rename");
plugAction(&menu, d->mActionCollection, "file_copy");
plugAction(&menu, d->mActionCollection, "file_move");
plugAction(&menu, d->mActionCollection, "file_link");
plugAction(&menu, d->mActionCollection, "file_delete");
menu.insertSeparator();
plugAction(&menu, d->mActionCollection, "file_properties");
}
menu.exec(pos);
}
} // namespace
|