viewmainpage.cpp 31.7 KB
Newer Older
1
2
/*
Gwenview: an image viewer
Aurélien Gâteau's avatar
Aurélien Gâteau committed
3
Copyright 2007 Aurélien Gâteau <agateau@kde.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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.

*/
David Edmundson's avatar
David Edmundson committed
20
#include "viewmainpage.h"
21
#include "config-gwenview.h"
22
23

// Qt
24
#include <QCheckBox>
25
#include <QItemSelectionModel>
26
#include <QShortcut>
27
#include <QVBoxLayout>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
28
#include <QMenu>
29

30
// KF
Aurélien Gâteau's avatar
Aurélien Gâteau committed
31
32
#include <KActionCollection>
#include <KActionCategory>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
33
#include <KLocalizedString>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
34
#include <KMessageBox>
35
#include <KModelIndexProxyMapper>
Aurélien Gâteau's avatar
Aurélien Gâteau committed
36
#include <KToggleAction>
37
#include <KSqueezedTextLabel>
38
39
40
#ifdef KF5Activities_FOUND
#include <KActivities/ResourceInstance>
#endif
Lukáš Tinkl's avatar
Lukáš Tinkl committed
41

42
// Local
43
#include "gwenview_app_debug.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
44
#include "fileoperations.h"
Aurélien Gâteau's avatar
Aurélien Gâteau committed
45
#include <gvcore.h>
46
#include "splitter.h"
47
#include <lib/documentview/abstractdocumentviewadapter.h>
48
#include <lib/documentview/abstractrasterimageviewtool.h>
49
#include <lib/documentview/documentview.h>
50
#include <lib/documentview/documentviewcontainer.h>
51
#include <lib/documentview/documentviewcontroller.h>
52
#include <lib/documentview/documentviewsynchronizer.h>
53
#include <lib/fullscreenbar.h>
54
#include <lib/gvdebug.h>
55
#include <lib/gwenviewconfig.h>
56
#include <lib/mimetypeutils.h>
57
#include <lib/paintutils.h>
58
#include <lib/semanticinfo/sorteddirmodel.h>
59
#include <lib/slidecontainer.h>
60
#include <lib/slideshow.h>
61
#include <lib/statusbartoolbutton.h>
62
#include <lib/thumbnailview/thumbnailbarview.h>
63
#include <lib/zoomwidget.h>
64
#include <lib/zoommode.h>
65
#include <lib/stylesheetutils.h>
66

67
68
namespace Gwenview
{
69

70
71
72
73
#undef ENABLE_LOG
#undef LOG
//#define ENABLE_LOG
#ifdef ENABLE_LOG
Laurent Montel's avatar
Laurent Montel committed
74
#define LOG(x) qCDebug(GWENVIEW_APP_LOG) << x
75
76
77
78
#else
#define LOG(x) ;
#endif

79
const int ViewMainPage::MaxViewCount = 6;
80

81
82
83
/*
 * Layout of mThumbnailSplitter is:
 *
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 * +-mThumbnailSplitter------------------------------------------------+
 * |+-mAdapterContainer-----------------------------------------------+|
 * ||+-mDocumentViewContainer----------------------------------------+||
 * |||+-DocumentView----------------++-DocumentView-----------------+|||
 * ||||                             ||                              ||||
 * ||||                             ||                              ||||
 * ||||                             ||                              ||||
 * ||||                             ||                              ||||
 * ||||                             ||                              ||||
 * ||||                             ||                              ||||
 * |||+-----------------------------++------------------------------+|||
 * ||+---------------------------------------------------------------+||
 * ||+-mToolContainer------------------------------------------------+||
 * |||                                                               |||
 * ||+---------------------------------------------------------------+||
 * |+-----------------------------------------------------------------+|
 * |===================================================================|
 * |+-mThumbnailBar---------------------------------------------------+|
 * ||                                                                 ||
 * ||                                                                 ||
104
105
 * |+-mStatusBarContainer---------------------------------------------+|
 * ||[mToggleSideBarButton][mToggleThumbnailBarButton]   [mZoomWidget]||
106
107
 * |+-----------------------------------------------------------------+|
 * +-------------------------------------------------------------------+
108
 */
Aurélien Gâteau's avatar
Aurélien Gâteau committed
109
110
struct ViewMainPagePrivate
{
111
    ViewMainPage* q;
112
113
    SlideShow* mSlideShow;
    KActionCollection* mActionCollection;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
114
    GvCore* mGvCore;
115
    KModelIndexProxyMapper* mDirModelToBarModelProxyMapper;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    QSplitter *mThumbnailSplitter;
    QWidget* mAdapterContainer;
    DocumentViewController* mDocumentViewController;
    QList<DocumentView*> mDocumentViews;
    DocumentViewSynchronizer* mSynchronizer;
    QToolButton* mToggleSideBarButton;
    QToolButton* mToggleThumbnailBarButton;
    ZoomWidget* mZoomWidget;
    DocumentViewContainer* mDocumentViewContainer;
    SlideContainer* mToolContainer;
    QWidget* mStatusBarContainer;
    ThumbnailBarView* mThumbnailBar;
    KToggleAction* mToggleThumbnailBarAction;
    KToggleAction* mSynchronizeAction;
130
    QCheckBox* mSynchronizeCheckBox;
131
    KSqueezedTextLabel* mDocumentCountLabel;
132

133
134
135
    // Activity Resource events reporting needs to be above KPart,
    // in the shell itself, to avoid problems with other MDI applications
    // that use this KPart
136
#ifdef KF5Activities_FOUND
137
    QHash<DocumentView*, KActivities::ResourceInstance*> mActivityResources;
138
#endif
139

140
    bool mCompareMode;
141
    ZoomMode::Enum mZoomMode;
142

143
    void setupWidgets()
144
    {
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        mToolContainer = new SlideContainer;
        mToolContainer->setAutoFillBackground(true);
        mToolContainer->setBackgroundRole(QPalette::Mid);
        //--
        mStatusBarContainer = new QWidget;
        mToggleSideBarButton = new StatusBarToolButton;
        mToggleThumbnailBarButton = new StatusBarToolButton;
        mZoomWidget = new ZoomWidget;
        mSynchronizeCheckBox = new QCheckBox(i18n("Synchronize"));
        mSynchronizeCheckBox->hide();
        mDocumentCountLabel = new KSqueezedTextLabel;
        mDocumentCountLabel->setAlignment(Qt::AlignCenter);
        mDocumentCountLabel->setTextElideMode(Qt::ElideRight);
        QMargins labelMargins = mDocumentCountLabel->contentsMargins();
        labelMargins.setLeft(15);
        labelMargins.setRight(15);
        mDocumentCountLabel->setContentsMargins(labelMargins);

        QHBoxLayout* statusBarContainerLayout = new QHBoxLayout(mStatusBarContainer);
164
        statusBarContainerLayout->setSizeConstraint(QLayout::SetFixedSize);
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        statusBarContainerLayout->setContentsMargins(0, 0, 0, 0);
        statusBarContainerLayout->setSpacing(0);
        statusBarContainerLayout->addWidget(mToggleSideBarButton);
        statusBarContainerLayout->addWidget(mToggleThumbnailBarButton);
        statusBarContainerLayout->addStretch();
        statusBarContainerLayout->addWidget(mSynchronizeCheckBox);
        // Ensure document count label takes up all available space,
        // so its autohide feature works properly (stretch factor = 1)
        statusBarContainerLayout->addWidget(mDocumentCountLabel, 1);
        statusBarContainerLayout->addStretch();
        statusBarContainerLayout->addWidget(mZoomWidget);
        //--
        mAdapterContainer = new QWidget;

        QVBoxLayout* adapterContainerLayout = new QVBoxLayout(mAdapterContainer);
        adapterContainerLayout->setContentsMargins(0, 0, 0, 0);
        adapterContainerLayout->setSpacing(0);
        mDocumentViewContainer = new DocumentViewContainer;
        mDocumentViewContainer->setAutoFillBackground(true);
        mDocumentViewContainer->setBackgroundRole(QPalette::Base);
        adapterContainerLayout->addWidget(mDocumentViewContainer);
        adapterContainerLayout->addWidget(mToolContainer);
        //--
188
189
190
191
        mThumbnailBar = new ThumbnailBarView;
        ThumbnailBarItemDelegate* delegate = new ThumbnailBarItemDelegate(mThumbnailBar);
        mThumbnailBar->setItemDelegate(delegate);
        mThumbnailBar->setSelectionMode(QAbstractItemView::ExtendedSelection);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
        //--
        Qt::Orientation orientation = GwenviewConfig::thumbnailBarOrientation();
        mThumbnailSplitter = new Splitter(orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal, q);
        mThumbnailBar->setOrientation(orientation);
        mThumbnailBar->setThumbnailAspectRatio(GwenviewConfig::thumbnailAspectRatio());
        mThumbnailBar->setRowCount(GwenviewConfig::thumbnailBarRowCount());
        mThumbnailSplitter->addWidget(mAdapterContainer);
        mThumbnailSplitter->addWidget(mThumbnailBar);
        mThumbnailSplitter->setSizes(GwenviewConfig::thumbnailSplitterSizes());
        // Show the thumbnail bar after setting the parent to avoid recreating
        // the native window and to avoid QTBUG-87345.
        mThumbnailBar->setVisible(GwenviewConfig::thumbnailBarIsVisible());

        QVBoxLayout* viewMainPageLayout = new QVBoxLayout(q);
        viewMainPageLayout->setContentsMargins(0, 0, 0, 0);
        viewMainPageLayout->setSpacing(0);
        viewMainPageLayout->addWidget(mThumbnailSplitter);
209
        viewMainPageLayout->addWidget(mStatusBarContainer);
210
211
212
213
214
        //--
        mDocumentViewController = new DocumentViewController(mActionCollection, q);
        mDocumentViewController->setZoomWidget(mZoomWidget);
        mDocumentViewController->setToolContainer(mToolContainer);
        mSynchronizer = new DocumentViewSynchronizer(&mDocumentViews, q);
215
216
217
218
    }

    void setupThumbnailBarStyleSheet()
    {
219
220
        QPalette pal = mGvCore->palette(GvCore::NormalViewPalette);
        mThumbnailBar->setPalette(pal);
221
        Qt::Orientation orientation = mThumbnailBar->orientation();
222
223
        QColor bgColor = pal.color(QPalette::Normal, QPalette::Base);
        QColor bgSelColor = pal.color(QPalette::Normal, QPalette::Highlight);
224
225
        QColor bgHovColor = pal.color(QPalette::Normal, QPalette::Highlight);
        
226
227
        // Avoid dark and bright colors
        bgColor.setHsv(bgColor.hue(), bgColor.saturation(), (127 + 3 * bgColor.value()) / 4);
228
229
230
        
        // Hover uses lighter/faded version of select color. Combine with bgColor to adapt to different backgrounds
        bgHovColor.setHsv(bgHovColor.hue(), (bgHovColor.saturation() / 2), ((bgHovColor.value() + bgColor.value()) / 2));
231
232
233
234
235
236

        QColor leftBorderColor = PaintUtils::adjustedHsv(bgColor, 0, 0, qMin(20, 255 - bgColor.value()));
        QColor rightBorderColor = PaintUtils::adjustedHsv(bgColor, 0, 0, -qMin(40, bgColor.value()));
        QColor borderSelColor = PaintUtils::adjustedHsv(bgSelColor, 0, 0, -qMin(60, bgSelColor.value()));

        QString itemCss =
237
            "QListView::item {"
238
239
240
241
242
            "	background-color: %1;"
            "	border-left: 1px solid %2;"
            "	border-right: 1px solid %3;"
            "}";
        itemCss = itemCss.arg(
243
244
245
                      StyleSheetUtils::gradient(orientation, bgColor, 46),
                      StyleSheetUtils::gradient(orientation, leftBorderColor, 36),
                      StyleSheetUtils::gradient(orientation, rightBorderColor, 26));
246
247
248
249
250
251
252
253

        QString itemSelCss =
            "QListView::item:selected {"
            "	background-color: %1;"
            "	border-left: 1px solid %2;"
            "	border-right: 1px solid %2;"
            "}";
        itemSelCss = itemSelCss.arg(
254
255
256
257
258
259
260
261
262
263
264
265
266
                         StyleSheetUtils::gradient(orientation, bgSelColor, 56),
                         StyleSheetUtils::rgba(borderSelColor));
        
        QString itemHovCss =
            "QListView::item:hover:!selected {"
            "  background-color: %1;"
            "  border-left: 1px solid %2;"
            "  border-right: 1px solid %3;"
            "}";
        itemHovCss = itemHovCss.arg(
                         StyleSheetUtils::gradient(orientation, bgHovColor, 56),
                         StyleSheetUtils::rgba(leftBorderColor),
                         StyleSheetUtils::rgba(rightBorderColor));
267

268
        QString css = itemCss + itemSelCss + itemHovCss;
269
270
271
272
273
274
275
276
277
278
279
280
        if (orientation == Qt::Vertical) {
            css.replace("left", "top").replace("right", "bottom");
        }

        mThumbnailBar->setStyleSheet(css);
    }

    DocumentView* createDocumentView()
    {
        DocumentView* view = mDocumentViewContainer->createView();

        // Connect context menu
281
        // If you need to connect another view signal, make sure it is disconnected in deleteDocumentView
Laurent Montel's avatar
Laurent Montel committed
282
283
284
285
286
        QObject::connect(view, &DocumentView::contextMenuRequested, q, &ViewMainPage::showContextMenu);

        QObject::connect(view, &DocumentView::completed, q, &ViewMainPage::completed);
        QObject::connect(view, &DocumentView::previousImageRequested, q, &ViewMainPage::previousImageRequested);
        QObject::connect(view, &DocumentView::nextImageRequested, q, &ViewMainPage::nextImageRequested);
287
288
        QObject::connect(view, &DocumentView::openUrlRequested, q, &ViewMainPage::openUrlRequested);
        QObject::connect(view, &DocumentView::openDirUrlRequested, q, &ViewMainPage::openDirUrlRequested);
Laurent Montel's avatar
Laurent Montel committed
289
290
291
292
293
294
295
        QObject::connect(view, &DocumentView::captionUpdateRequested, q, &ViewMainPage::captionUpdateRequested);
        QObject::connect(view, &DocumentView::toggleFullScreenRequested, q, &ViewMainPage::toggleFullScreenRequested);
        QObject::connect(view, &DocumentView::focused, q, &ViewMainPage::slotViewFocused);
        QObject::connect(view, &DocumentView::hudTrashClicked, q, &ViewMainPage::trashView);
        QObject::connect(view, &DocumentView::hudDeselectClicked, q, &ViewMainPage::deselectView);

        QObject::connect(view, &DocumentView::videoFinished, mSlideShow, &SlideShow::resumeAndGoToNextUrl);
296
297

        mDocumentViews << view;
298
#ifdef KF5Activities_FOUND
299
        mActivityResources.insert(view, new KActivities::ResourceInstance(q->window()->winId(), view));
300
#endif
301
302
303
304

        return view;
    }

305
    void deleteDocumentView(DocumentView* view)
306
    {
307
        if (mDocumentViewController->view() == view) {
308
            mDocumentViewController->setView(nullptr);
309
        }
310
311
312
313
314

        // Make sure we do not get notified about this view while it is going away.
        // mDocumentViewController->deleteView() animates the view deletion so
        // the view still exists for a short while when we come back to the
        // event loop)
315
316
        QObject::disconnect(view, nullptr, q, nullptr);
        QObject::disconnect(view, nullptr, mSlideShow, nullptr);
317

318
        mDocumentViews.removeOne(view);
319
#ifdef KF5Activities_FOUND
320
        mActivityResources.remove(view);
321
#endif
322
        mDocumentViewContainer->deleteView(view);
323
324
325
326
327
328
329
330
331
    }

    void saveSplitterConfig()
    {
        if (mThumbnailBar->isVisible()) {
            GwenviewConfig::setThumbnailSplitterSizes(mThumbnailSplitter->sizes());
        }
    }

Aurélien Gâteau's avatar
Aurélien Gâteau committed
332
333
    DocumentView* currentView() const
    {
334
335
336
337
338
339
340
341
342
343
344
        return mDocumentViewController->view();
    }

    void setCurrentView(DocumentView* view)
    {
        DocumentView* oldView = currentView();
        if (view == oldView) {
            return;
        }
        if (oldView) {
            oldView->setCurrent(false);
345
#ifdef KF5Activities_FOUND
346
            Q_ASSERT(mActivityResources.contains(oldView));
347
            mActivityResources.value(oldView)->notifyFocusedOut();
348
#endif
349
350
351
352
353
354
        }
        view->setCurrent(true);
        mDocumentViewController->setView(view);
        mSynchronizer->setCurrentView(view);

        QModelIndex index = indexForView(view);
355
356
357
358
359
        if (index.isValid()) {
            // Index may be invalid when Gwenview is started as
            // `gwenview /foo/image.png` because in this situation it loads image.png
            // *before* listing /foo (because it matters less to the user)
            mThumbnailBar->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Current);
360
        }
361
#ifdef KF5Activities_FOUND
362
        Q_ASSERT(mActivityResources.contains(view));
363
        mActivityResources.value(view)->notifyFocusedIn();
364
#endif
365
366
        QObject::connect(view, &DocumentView::currentToolChanged,
                         q, &ViewMainPage::updateFocus);
367
368
    }

Aurélien Gâteau's avatar
Aurélien Gâteau committed
369
370
    QModelIndex indexForView(DocumentView* view) const
    {
David Edmundson's avatar
David Edmundson committed
371
        QUrl url = view->url();
372
        if (!url.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
373
            qCWarning(GWENVIEW_APP_LOG) << "View does not display any document!";
374
375
376
            return QModelIndex();
        }

377
378
379
380
381
382
383
384
385
386
        SortedDirModel* dirModel = mGvCore->sortedDirModel();
        QModelIndex srcIndex = dirModel->indexForUrl(url);
        if (!mDirModelToBarModelProxyMapper) {
            // Delay the initialization of the mapper to its first use because
            // mThumbnailBar->model() is not set after ViewMainPage ctor is
            // done.
            const_cast<ViewMainPagePrivate*>(this)->mDirModelToBarModelProxyMapper = new KModelIndexProxyMapper(dirModel, mThumbnailBar->model(), q);
        }
        QModelIndex index = mDirModelToBarModelProxyMapper->mapLeftToRight(srcIndex);
        return index;
387
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
388
389
390

    void applyPalette(bool fullScreenMode)
    {
391
        mDocumentViewContainer->applyPalette(mGvCore->palette(fullScreenMode ? GvCore::FullScreenViewPalette : GvCore::NormalViewPalette));
Aurélien Gâteau's avatar
Aurélien Gâteau committed
392
393
        setupThumbnailBarStyleSheet();
    }
394
395
396
397
398
399
400
401

    void updateDocumentCountLabel()
    {
        const int current = mThumbnailBar->currentIndex().row() + 1;  // zero-based
        const int total = mThumbnailBar->model()->rowCount();
        const QString text = i18nc("@info:status %1 current document index, %2 total documents", "%1 of %2", current, total);
        mDocumentCountLabel->setText(text);
    }
402
403
};

Aurélien Gâteau's avatar
Aurélien Gâteau committed
404
ViewMainPage::ViewMainPage(QWidget* parent, SlideShow* slideShow, KActionCollection* actionCollection, GvCore* gvCore)
405
: QWidget(parent)
406
, d(new ViewMainPagePrivate)
407
{
408
    d->q = this;
409
    d->mDirModelToBarModelProxyMapper = nullptr; // Initialized later
410
411
    d->mSlideShow = slideShow;
    d->mActionCollection = actionCollection;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
412
    d->mGvCore = gvCore;
413
414
    d->mCompareMode = false;

415
416
    QShortcut* enterKeyShortcut = new QShortcut(Qt::Key_Return, this);
    connect(enterKeyShortcut, &QShortcut::activated, this, &ViewMainPage::slotEnterPressed);
417

418
    d->setupWidgets();
419
420
421

    KActionCategory* view = new KActionCategory(i18nc("@title actions category - means actions changing smth in interface", "View"), actionCollection);

422
    d->mToggleThumbnailBarAction = view->add<KToggleAction>(QStringLiteral("toggle_thumbnailbar"));
423
    d->mToggleThumbnailBarAction->setText(i18n("Thumbnail Bar"));
Laurent Montel's avatar
Laurent Montel committed
424
    d->mToggleThumbnailBarAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-image")));
425
    actionCollection->setDefaultShortcut(d->mToggleThumbnailBarAction, Qt::CTRL | Qt::Key_B);
Laurent Montel's avatar
Laurent Montel committed
426
    connect(d->mToggleThumbnailBarAction, &KToggleAction::triggered, this, &ViewMainPage::setThumbnailBarVisibility);
427
428
429
430
    d->mToggleThumbnailBarButton->setDefaultAction(d->mToggleThumbnailBarAction);

    d->mSynchronizeAction = view->add<KToggleAction>("synchronize_views");
    d->mSynchronizeAction->setText(i18n("Synchronize"));
431
    actionCollection->setDefaultShortcut(d->mSynchronizeAction, Qt::CTRL | Qt::Key_Y);
Laurent Montel's avatar
Laurent Montel committed
432
433
    connect(d->mSynchronizeAction, &QAction::toggled,
            d->mSynchronizer, &DocumentViewSynchronizer::setActive);
434
    // Ensure mSynchronizeAction and mSynchronizeCheckBox are in sync
Laurent Montel's avatar
Laurent Montel committed
435
436
437
438
    connect(d->mSynchronizeAction, &QAction::toggled,
            d->mSynchronizeCheckBox, &QAbstractButton::setChecked);
    connect(d->mSynchronizeCheckBox, &QAbstractButton::toggled,
            d->mSynchronizeAction, &QAction::setChecked);
439

440
441
442
443
444
445
    // Connections for the document count
    connect(d->mThumbnailBar, &ThumbnailBarView::rowsInsertedSignal,
            this, &ViewMainPage::slotDirModelItemsAddedOrRemoved);
    connect(d->mThumbnailBar, &ThumbnailBarView::rowsRemovedSignal,
            this, &ViewMainPage::slotDirModelItemsAddedOrRemoved);

446
    installEventFilter(this);
447
448
}

449
ViewMainPage::~ViewMainPage()
450
451
{
    delete d;
452
453
}

454
void ViewMainPage::loadConfig()
455
{
456
    d->applyPalette(window()->isFullScreen());
Aurélien Gâteau's avatar
Aurélien Gâteau committed
457

458
    // FIXME: Not symmetric with saveConfig(). Check if it matters.
459
    for (DocumentView * view : qAsConst(d->mDocumentViews)) {
460
461
462
463
464
465
466
        view->loadAdapterConfig();
    }

    Qt::Orientation orientation = GwenviewConfig::thumbnailBarOrientation();
    d->mThumbnailSplitter->setOrientation(orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal);
    d->mThumbnailBar->setOrientation(orientation);
    d->setupThumbnailBarStyleSheet();
467
468
    d->mThumbnailBar->setVisible(GwenviewConfig::thumbnailBarIsVisible());
    d->mToggleThumbnailBarAction->setChecked(GwenviewConfig::thumbnailBarIsVisible());
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

    int oldRowCount = d->mThumbnailBar->rowCount();
    int newRowCount = GwenviewConfig::thumbnailBarRowCount();
    if (oldRowCount != newRowCount) {
        d->mThumbnailBar->setUpdatesEnabled(false);
        int gridSize = d->mThumbnailBar->gridSize().width();

        d->mThumbnailBar->setRowCount(newRowCount);

        // Adjust splitter to ensure thumbnail size remains the same
        int delta = (newRowCount - oldRowCount) * gridSize;
        QList<int> sizes = d->mThumbnailSplitter->sizes();
        Q_ASSERT(sizes.count() == 2);
        sizes[0] -= delta;
        sizes[1] += delta;
        d->mThumbnailSplitter->setSizes(sizes);

        d->mThumbnailBar->setUpdatesEnabled(true);
    }
488

489
    d->mZoomMode = GwenviewConfig::zoomMode();
490
491
}

492
void ViewMainPage::saveConfig()
493
494
495
{
    d->saveSplitterConfig();
    GwenviewConfig::setThumbnailBarIsVisible(d->mToggleThumbnailBarAction->isChecked());
496
497
}

498
void ViewMainPage::setThumbnailBarVisibility(bool visible)
499
500
501
{
    d->saveSplitterConfig();
    d->mThumbnailBar->setVisible(visible);
502
503
}

504
int ViewMainPage::statusBarHeight() const
505
506
{
    return d->mStatusBarContainer->height();
507
508
}

Valerio Pilo's avatar
Valerio Pilo committed
509
510
511
512
513
void ViewMainPage::setStatusBarVisible(bool visible)
{
    d->mStatusBarContainer->setVisible(visible);
}

514
void ViewMainPage::setFullScreenMode(bool fullScreenMode)
515
516
{
    if (fullScreenMode) {
517
        d->mThumbnailBar->setVisible(false);
518
    } else {
519
        d->mThumbnailBar->setVisible(d->mToggleThumbnailBarAction->isChecked());
520
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
521
    d->applyPalette(fullScreenMode);
522
    d->mToggleThumbnailBarAction->setEnabled(!fullScreenMode);
523
524
}

525
ThumbnailBarView* ViewMainPage::thumbnailBar() const
526
527
{
    return d->mThumbnailBar;
Aurélien Gâteau's avatar
Aurélien Gâteau committed
528
529
}

David Edmundson's avatar
David Edmundson committed
530
inline void addActionToMenu(QMenu* menu, KActionCollection* actionCollection, const char* name)
531
532
533
534
535
{
    QAction* action = actionCollection->action(name);
    if (action) {
        menu->addAction(action);
    }
Aurélien Gâteau's avatar
Aurélien Gâteau committed
536
537
}

538
void ViewMainPage::showContextMenu()
539
{
David Edmundson's avatar
David Edmundson committed
540
    QMenu menu(this);
541
542
543
544
545
546
547
548
549
550
    addActionToMenu(&menu, d->mActionCollection, "fullscreen");
    menu.addSeparator();
    addActionToMenu(&menu, d->mActionCollection, "go_previous");
    addActionToMenu(&menu, d->mActionCollection, "go_next");
    if (d->currentView()->canZoom()) {
        menu.addSeparator();
        addActionToMenu(&menu, d->mActionCollection, "view_actual_size");
        addActionToMenu(&menu, d->mActionCollection, "view_zoom_to_fit");
        addActionToMenu(&menu, d->mActionCollection, "view_zoom_in");
        addActionToMenu(&menu, d->mActionCollection, "view_zoom_out");
551
        addActionToMenu(&menu, d->mActionCollection, "view_toggle_birdeyeview");
552
553
554
555
556
557
558
559
560
561
562
563
    }
    if (d->mCompareMode) {
        menu.addSeparator();
        addActionToMenu(&menu, d->mActionCollection, "synchronize_views");
    }

    menu.addSeparator();
    addActionToMenu(&menu, d->mActionCollection, "file_copy_to");
    addActionToMenu(&menu, d->mActionCollection, "file_move_to");
    addActionToMenu(&menu, d->mActionCollection, "file_link_to");
    menu.addSeparator();
    addActionToMenu(&menu, d->mActionCollection, "file_open_with");
564
565
    addActionToMenu(&menu, d->mActionCollection, "file_open_containing_folder");
    
566
    menu.exec(QCursor::pos());
Aurélien Gâteau's avatar
Aurélien Gâteau committed
567
568
}

569
QSize ViewMainPage::sizeHint() const
570
571
{
    return QSize(400, 300);
572
573
}

574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
QSize ViewMainPage::minimumSizeHint() const {
    if (!layout()) {
        return QSize();
    }

    QSize minimumSize = layout()->minimumSize();
    if (window()->isFullScreen()) {
        // Check minimum width of the overlay fullscreen bar
        // since there is no layout link which could do this
        const FullScreenBar* fullScreenBar = findChild<FullScreenBar*>();
        if (fullScreenBar && fullScreenBar->layout()) {
            const int fullScreenBarWidth = fullScreenBar->layout()->minimumSize().width();
            if (fullScreenBarWidth > minimumSize.width()) {
                minimumSize.setWidth(fullScreenBarWidth);
            }
        }
    }
    return minimumSize;
}

David Edmundson's avatar
David Edmundson committed
594
QUrl ViewMainPage::url() const
595
{
David Edmundson's avatar
David Edmundson committed
596
    GV_RETURN_VALUE_IF_FAIL(d->currentView(), QUrl());
597
    return d->currentView()->url();
598
599
}

600
Document::Ptr ViewMainPage::currentDocument() const
601
602
603
604
605
{
    if (!d->currentView()) {
        LOG("!d->documentView()");
        return Document::Ptr();
    }
606

607
    return d->currentView()->document();
608
609
}

610
bool ViewMainPage::isEmpty() const
611
{
612
    return !currentDocument();
613
614
}

615
RasterImageView* ViewMainPage::imageView() const
616
617
{
    if (!d->currentView()) {
618
        return nullptr;
619
620
    }
    return d->currentView()->imageView();
621
622
}

623
DocumentView* ViewMainPage::documentView() const
624
625
{
    return d->currentView();
626
627
}

David Edmundson's avatar
David Edmundson committed
628
void ViewMainPage::openUrl(const QUrl &url)
629
{
David Edmundson's avatar
David Edmundson committed
630
    openUrls(QList<QUrl>() << url, url);
631
632
}

David Edmundson's avatar
David Edmundson committed
633
void ViewMainPage::openUrls(const QList<QUrl>& allUrls, const QUrl &currentUrl)
634
{
635
    DocumentView::Setup setup;
636

637
638
639
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    QSet<QUrl> urls(allUrls.begin(), allUrls.end());
#else
David Edmundson's avatar
David Edmundson committed
640
    QSet<QUrl> urls = allUrls.toSet();
641
#endif
642
643
    d->mCompareMode = urls.count() > 1;

David Edmundson's avatar
David Edmundson committed
644
    typedef QMap<QUrl, DocumentView*> ViewForUrlMap;
645
646
    ViewForUrlMap viewForUrlMap;

647
648
649
    if (!d->mDocumentViews.isEmpty()) {
        d->mDocumentViewContainer->updateSetup(d->mDocumentViews.last());
    }
650
    if (d->mDocumentViews.isEmpty() || d->mZoomMode == ZoomMode::Autofit) {
651
        setup.valid = true;
652
        setup.zoomToFit = true;
653
654
    } else {
        setup = d->mDocumentViews.last()->setup();
655
    }
656
657
    // Destroy views which show urls we don't care about, remove from "urls" the
    // urls which already have a view.
658
    for (DocumentView * view : qAsConst(d->mDocumentViews)) {
David Edmundson's avatar
David Edmundson committed
659
        QUrl url = view->url();
660
661
662
        if (urls.contains(url)) {
            // view displays an url we must display, keep it
            urls.remove(url);
663
            viewForUrlMap.insert(url, view);
664
665
        } else {
            // view url is not interesting, drop it
666
            d->deleteDocumentView(view);
667
668
669
670
        }
    }

    // Create view for remaining urls
671
    for (const QUrl &url : qAsConst(urls)) {
672
        if (d->mDocumentViews.count() >= MaxViewCount) {
Laurent Montel's avatar
Laurent Montel committed
673
            qCWarning(GWENVIEW_APP_LOG) << "Too many documents to show";
674
675
676
            break;
        }
        DocumentView* view = d->createDocumentView();
677
        viewForUrlMap.insert(url, view);
678
679
    }

680
681
    // Set sortKey to match url order
    int sortKey = 0;
682
    for (const QUrl &url : allUrls) {
683
684
685
686
        viewForUrlMap[url]->setSortKey(sortKey);
        ++sortKey;
    }

687
688
689
690
691
    d->mDocumentViewContainer->updateLayout();

    // Load urls for new views. Do it only now because the view must have the
    // correct size before it starts loading its url. Do not do it later because
    // view->url() needs to be set for the next loop.
692
    ViewForUrlMap::ConstIterator
693
694
        it = viewForUrlMap.constBegin(),
        end = viewForUrlMap.constEnd();
695
    for (; it != end; ++it) {
David Edmundson's avatar
David Edmundson committed
696
        QUrl url = it.key();
697
        DocumentView* view = it.value();
698
        DocumentView::Setup savedSetup = d->mDocumentViewContainer->savedSetup(url);
699
        view->openUrl(url, d->mZoomMode == ZoomMode::Individual && savedSetup.valid ? savedSetup : setup);
700
#ifdef KF5Activities_FOUND
701
702
703
704
        if (GwenviewConfig::historyEnabled()) {
            d->mActivityResources.value(view)->setUri(url);
            d->mActivityResources.value(view)->setMimetype(MimeTypeUtils::urlMimeType(url));
        }
705
#endif
706
707
708
    }

    // Init views
709
    for (DocumentView * view : qAsConst(d->mDocumentViews)) {
710
711
712
713
714
715
716
717
718
719
720
721
722
723
        view->setCompareMode(d->mCompareMode);
        if (view->url() == currentUrl) {
            d->setCurrentView(view);
        } else {
            view->setCurrent(false);
        }
    }

    d->mSynchronizeCheckBox->setVisible(d->mCompareMode);
    if (d->mCompareMode) {
        d->mSynchronizer->setActive(d->mSynchronizeCheckBox->isChecked());
    } else {
        d->mSynchronizer->setActive(false);
    }
724
725
726

    d->updateDocumentCountLabel();
    d->mDocumentCountLabel->setVisible(!d->mCompareMode);
727
728
}

729
void ViewMainPage::reload()
730
{
731
732
733
734
735
    DocumentView *view = d->currentView();
    if (!view) {
        return;
    }
    Document::Ptr doc = view->document();
736
    if (!doc) {
Laurent Montel's avatar
Laurent Montel committed
737
        qCWarning(GWENVIEW_APP_LOG) << "!doc";
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
        return;
    }
    if (doc->isModified()) {
        KGuiItem cont = KStandardGuiItem::cont();
        cont.setText(i18nc("@action:button", "Discard Changes and Reload"));
        int answer = KMessageBox::warningContinueCancel(this,
                     i18nc("@info", "This image has been modified. Reloading it will discard all your changes."),
                     QString() /* caption */,
                     cont);
        if (answer != KMessageBox::Continue) {
            return;
        }
    }
    doc->reload();
    // Call openUrl again because DocumentView may need to switch to a new
    // adapter (for example because document was broken and it is not anymore)
754
    d->currentView()->openUrl(doc->url(), d->currentView()->setup());
Aurélien Gâteau's avatar
Aurélien Gâteau committed
755
756
}

757
void ViewMainPage::reset()
758
{
759
    d->mDocumentViewController->reset();
760
761
    d->mDocumentViewContainer->reset();
    d->mDocumentViews.clear();
762
763
}

764
void ViewMainPage::slotViewFocused(DocumentView* view)
765
766
{
    d->setCurrentView(view);
767
768
}

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
void ViewMainPage::slotEnterPressed()
{
    DocumentView *view = d->currentView();
    if (view) {
        AbstractRasterImageViewTool *tool = view->currentTool();
        if (tool) {
            QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
            tool->keyPressEvent(&event);
            if (event.isAccepted()) {
                return;
            }
        }
    }
    emit goToBrowseModeRequested();
}

785
bool ViewMainPage::eventFilter(QObject* watched, QEvent* event)
786
{
787
    if (event->type() == QEvent::ShortcutOverride) {
788
789
        const int key = static_cast<QKeyEvent*>(event)->key();
        if (key == Qt::Key_Space || key == Qt::Key_Escape) {
790
791
792
793
            const DocumentView* view = d->currentView();
            if (view) {
                AbstractRasterImageViewTool* tool = view->currentTool();
                if (tool) {
794
                    QKeyEvent toolKeyEvent(QEvent::KeyPress, key, Qt::NoModifier);
795
796
797
798
799
                    tool->keyPressEvent(&toolKeyEvent);
                    if (toolKeyEvent.isAccepted()) {
                        event->accept();
                    }
                }
800
801
            }
        }
802
803
804
805
806
        // Leave fullscreen when viewing an image
        if (window()->isFullScreen() && key == Qt::Key_Escape) {
            d->mActionCollection->action("leave_fullscreen")->trigger();
            event->accept();
        }
807
    }
808
809
    
    return QWidget::eventFilter(watched, event);
810
811
}

812
void ViewMainPage::trashView(DocumentView* view)
813
{
David Edmundson's avatar
David Edmundson committed
814
    QUrl url = view->url();
815
    deselectView(view);
David Edmundson's avatar
David Edmundson committed
816
    FileOperations::trash(QList<QUrl>() << url, this);
817
818
}

819
void ViewMainPage::deselectView(DocumentView* view)
820
{
821
    DocumentView* newCurrentView = nullptr;
822
823
824
    if (view == d->currentView()) {
        // We need to find a new view to set as current
        int idx = d->mDocumentViews.indexOf(view);
Aurélien Gâteau's avatar
Aurélien Gâteau committed
825
826
827
828
829
        if (idx + 1 < d->mDocumentViews.count()) {
            newCurrentView = d->mDocumentViews.at(idx + 1);
        } else if (idx > 0) {
            newCurrentView = d->mDocumentViews.at(idx - 1);
        } else {
830
            GV_WARN_AND_RETURN("No view found to set as current");
831
832
833
834
835
836
837
838
839
840
        }
    }

    QModelIndex index = d->indexForView(view);
    QItemSelectionModel* selectionModel = d->mThumbnailBar->selectionModel();
    selectionModel->select(index, QItemSelectionModel::Deselect);

    if (newCurrentView) {
        d->setCurrentView(newCurrentView);
    }
841
842
}

843
QToolButton* ViewMainPage::toggleSideBarButton() const
844
845
{
    return d->mToggleSideBarButton;
846
847
}

848
void ViewMainPage::showMessageWidget(QGraphicsWidget* widget, Qt::Alignment align)
849
{
850
    d->mDocumentViewContainer->showMessageWidget(widget, align);
851
852
}

853
854
855
856
857
858
859
void ViewMainPage::updateFocus(const AbstractRasterImageViewTool* tool)
{
    if (!tool) {
        d->mDocumentViewContainer->setFocus();
    }
}

860
861
862
863
864
void ViewMainPage::slotDirModelItemsAddedOrRemoved()
{
    d->updateDocumentCountLabel();
}

865
} // namespace