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
|
/* This file is part of the Calligra project, made within the KDE community.
*
* Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoPageNavigator.h"
#include "KoPageNavigatorButton_p.h"
#include <KoPAView.h>
#include <KoPADocument.h>
#include <KoPAPage.h>
#include <KoIcon.h>
// KDE
#include <kdebug.h>
#include <klocale.h>
#include <kactioncollection.h>
// Qt
#include <QLabel>
#include <QHBoxLayout>
#include <QWheelEvent>
#include <QEvent>
#include <QLineEdit>
#include <QIntValidator>
#include <QAction>
static const int maxPageCountPattern = 999;
class KoPageNavigator::Private
{
public:
explicit Private(KoPAView *_view)
: view(_view)
{}
// normal display
QLabel* displayLabel;
// interactive state
KoPageNavigatorButton *gotoFirstPageButton;
KoPageNavigatorButton *gotoPreviousPageButton;
KoPageNavigatorButton *gotoNextPageButton;
KoPageNavigatorButton *gotoLastPageButton;
QLineEdit *pageNumberEdit;
QIntValidator *pageNumberEditValidator;
KoPAView *view;
};
static QString
displayText(bool isMaster, bool isSlideType, int pageNumber, int pageCount)
{
return isSlideType ?
(isMaster ? i18n("Master Slide %1/%2", pageNumber, pageCount)
: i18n("Slide %1/%2", pageNumber, pageCount)) :
(isMaster ? i18n("Master Page %1/%2", pageNumber, pageCount)
: i18n("Page %1/%2", pageNumber, pageCount));
}
KoPageNavigator::KoPageNavigator(KoPAView *view)
: QStackedWidget(view)
, d(new Private(view))
{
const bool isSlideType = (d->view->kopaDocument()->pageType() == KoPageApp::Slide);
#ifdef Q_WS_MAC
setAttribute(Qt::WA_MacMiniSize, true);
#endif
// normal display
d->displayLabel = new QLabel(this);
d->displayLabel->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
addWidget(d->displayLabel);
// add interactive variant
QWidget* controlWidget = new QWidget(this);
QHBoxLayout* layout = new QHBoxLayout(controlWidget);
layout->setSpacing(0);
layout->setMargin(0);
// the original go-*-view-page icons as set for the actions are not reused,
// because they look too complex, at least with the Oxygen icons
// also installing an event filter for all buttons, to get wheel events even
// for disabled buttons
d->gotoFirstPageButton = new KoPageNavigatorButton(koIconNameCStr("go-first-view"), this);
d->gotoFirstPageButton->installEventFilter(this);
d->gotoPreviousPageButton = new KoPageNavigatorButton(koIconNameCStr("go-previous-view"), this);
d->gotoPreviousPageButton->installEventFilter(this);
d->gotoNextPageButton = new KoPageNavigatorButton(koIconNameCStr("go-next-view"), this);
d->gotoNextPageButton->installEventFilter(this);
d->gotoLastPageButton = new KoPageNavigatorButton(koIconNameCStr("go-last-view"), this);
d->gotoLastPageButton->installEventFilter(this);
d->pageNumberEdit = new QLineEdit(this);
d->pageNumberEdit->installEventFilter(this);
d->pageNumberEditValidator = new QIntValidator(d->pageNumberEdit);
d->pageNumberEditValidator->setBottom(1);
d->pageNumberEdit->setValidator(d->pageNumberEditValidator);
d->pageNumberEdit->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
connect(d->pageNumberEdit, SIGNAL(returnPressed()), SLOT(onPageNumberEntered()));
layout->addWidget(d->gotoFirstPageButton);
layout->addWidget(d->gotoPreviousPageButton);
layout->addWidget(d->pageNumberEdit);
layout->addWidget(d->gotoNextPageButton);
layout->addWidget(d->gotoLastPageButton);
addWidget(controlWidget);
KoPADocument *const kopaDocument = d->view->kopaDocument();
connect(kopaDocument, SIGNAL(pageAdded(KoPAPageBase*)), SLOT(updateDisplayLabel()));
connect(kopaDocument, SIGNAL(pageRemoved(KoPAPageBase*)), SLOT(updateDisplayLabel()));
connect(d->view->proxyObject, SIGNAL(activePageChanged()), SLOT(updateDisplayLabel()));
// Fix width by the largest needed
QFontMetrics fontMetrics(font());
d->pageNumberEdit->setMinimumWidth(fontMetrics.width(QString::number(maxPageCountPattern*10))); //one more
const int editWidth = widget(Edit)->minimumWidth();
const int normalWidth = fontMetrics.width(displayText(false, isSlideType, maxPageCountPattern, maxPageCountPattern));
const int masterWidth = fontMetrics.width(displayText(true, isSlideType, maxPageCountPattern, maxPageCountPattern));
setFixedWidth(qMax(editWidth, qMax(normalWidth, masterWidth)));
updateDisplayLabel();
}
KoPageNavigator::~KoPageNavigator()
{
delete d;
}
void KoPageNavigator::initActions()
{
KActionCollection *actionCollection = d->view->actionCollection();
d->gotoFirstPageButton->setAction(actionCollection->action(QLatin1String("page_first")));
d->gotoPreviousPageButton->setAction(actionCollection->action(QLatin1String("page_previous")));
d->gotoNextPageButton->setAction(actionCollection->action(QLatin1String("page_next")));
d->gotoLastPageButton->setAction(actionCollection->action(QLatin1String("page_last")));
}
void KoPageNavigator::enterEvent(QEvent *event)
{
Q_UNUSED(event);
setCurrentIndex(Edit);
}
void KoPageNavigator::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
if (! d->pageNumberEdit->hasFocus()) {
setCurrentIndex(Display);
}
}
bool KoPageNavigator::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::FocusOut && watched == d->pageNumberEdit) {
if (! underMouse()) {
setCurrentIndex(Display);
}
// reset editor in any case
KoPADocument *const kopaDocument = d->view->kopaDocument();
KoPAPageBase *const activePage = d->view->activePage();
const int pageNumber = kopaDocument->pageIndex(activePage) + 1;
const QString text = (pageNumber > 0) ? QString::number(pageNumber) : QString();
d->pageNumberEdit->setText(text);
} else if (event->type() == QEvent::Wheel) {
// Scroll the pages by the wheel
// Because the numbers are representatives of the actual pages
// and the list of pages is ordered by smaller number first,
// here an increasing delta means going up in the list, so go to
// smaller page numbers, and vice versa.
QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
const int delta = wheelEvent->delta();
// trigger the respective actions
if (delta > 0) {
QAction *gotoPreviousPageAction = d->gotoPreviousPageButton->action();
if (gotoPreviousPageAction->isEnabled()) {
gotoPreviousPageAction->activate(QAction::Trigger);
}
} else if (delta < 0) {
QAction *gotoNextPageAction = d->gotoNextPageButton->action();
if (gotoNextPageAction->isEnabled()) {
gotoNextPageAction->activate(QAction::Trigger);
}
}
// scroll wheel events also cancel the editing,
// so move focus out of the pageNumberEdit
if (d->pageNumberEdit->hasFocus()) {
d->view->setFocus();
}
}
return false;
}
void KoPageNavigator::updateDisplayLabel()
{
KoPADocument *const kopaDocument = d->view->kopaDocument();
KoPAPageBase *const activePage = d->view->activePage();
const int pageNumber = kopaDocument->pageIndex(activePage) + 1;
if (pageNumber > 0) {
const bool isMasterPage = (dynamic_cast<KoPAPage*>(activePage) == 0);
const int pageCount = d->view->kopaDocument()->pages(isMasterPage).size();
const bool isSlideType = (d->view->kopaDocument()->pageType() == KoPageApp::Slide);
d->displayLabel->setText(displayText(isMasterPage, isSlideType, pageNumber, pageCount));
d->pageNumberEdit->setText(QString::number(pageNumber));
d->pageNumberEditValidator->setTop(pageCount);
}
// also leave the editor if in it
if (d->pageNumberEdit->hasFocus()) {
d->view->setFocus();
}
}
void KoPageNavigator::onPageNumberEntered()
{
const int pageNumber = d->pageNumberEdit->text().toInt();
KoPADocument *const kopaDocument = d->view->kopaDocument();
KoPAPageBase *const activePage = d->view->activePage();
const bool isMasterPage = (dynamic_cast<KoPAPage*>(activePage) == 0);
const QList<KoPAPageBase*> pages = kopaDocument->pages(isMasterPage);
KoPAPageBase* newPage = pages.value(pageNumber-1);
if (newPage) {
d->view->proxyObject->updateActivePage(newPage);
}
}
|