File indexing completed on 2024-05-19 16:09:56

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2001 David Faure <faure@kde.org>
0003  * Copyright (C) 2005-2007, 2009 Thomas Zander <zander@kde.org>
0004  * Copyright (C) 2010 Boudewijn Rempt <boud@kogmbh.com>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA
0020  */
0021 
0022 #include "KWGui.h"
0023 #include "KWView.h"
0024 #include "KWDocument.h"
0025 #include "KWCanvas.h"
0026 #include "KWPage.h"
0027 
0028 #include <KoCanvasControllerWidget.h>
0029 #include <KoToolManager.h>
0030 #include <KoShapeManager.h>
0031 #include <KoShape.h>
0032 #include <KoFlake.h>
0033 #include <KoSelection.h>
0034 #include <KoDockerManager.h>
0035 #include <KoRuler.h>
0036 #include <KoUnit.h>
0037 #include <KoModeBoxFactory.h>
0038 #include <KoRulerController.h>
0039 #include <kactioncollection.h>
0040 
0041 #include <QGridLayout>
0042 #include <QTimer>
0043 #include <QAction>
0044 #include <QScrollBar>
0045 #include <KoMainWindow.h>
0046 
0047 KWGui::KWGui(const QString &viewMode, KWView *parent)
0048         : QWidget(parent),
0049         m_view(parent)
0050 {
0051     QGridLayout *gridLayout = new QGridLayout(this);
0052     gridLayout->setMargin(0);
0053     gridLayout->setSpacing(0);
0054 
0055     setMouseTracking(true);
0056 
0057     // Ruler
0058     m_horizontalRuler = new KoRuler(this, Qt::Horizontal, m_view->viewConverter());
0059     m_horizontalRuler->setShowMousePosition(true);
0060     m_horizontalRuler->setUnit(m_view->kwdocument()->unit());
0061     m_verticalRuler = new KoRuler(this, Qt::Vertical, m_view->viewConverter());
0062     m_verticalRuler->setUnit(m_view->kwdocument()->unit());
0063     m_verticalRuler->setShowMousePosition(true);
0064 
0065     m_canvas = new KWCanvas(viewMode, static_cast<KWDocument*>(m_view->koDocument()), m_view, this);
0066     KoCanvasControllerWidget *canvasController = new KoCanvasControllerWidget(m_view->actionCollection(), this);
0067     m_canvasController = canvasController;
0068     // We need to set this as QDeclarativeView sets them a bit different from QAbstractScrollArea
0069     canvasController->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
0070     canvasController->setFocusPolicy(Qt::NoFocus);
0071         //setScene(0);
0072 
0073     canvasController->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
0074     m_canvasController->setMargin(10);
0075     m_canvasController->setCanvas(m_canvas);
0076     m_canvasController->setCanvasMode(KoCanvasController::AlignTop);
0077     KoToolManager::instance()->addController(m_canvasController);
0078     KoToolManager::instance()->registerTools(m_view->actionCollection(), m_canvasController);
0079 
0080     if (m_view->mainWindow())
0081     {
0082         KoModeBoxFactory modeBoxFactory(canvasController, qApp->applicationName(), i18n("Tools"));
0083         QDockWidget* modeBox = m_view->mainWindow()->createDockWidget(&modeBoxFactory);
0084         m_view->mainWindow()->dockerManager()->removeToolOptionsDocker();
0085         dynamic_cast<KoCanvasObserverBase*>(modeBox)->setObservedCanvas(m_canvas);
0086     }
0087 
0088     gridLayout->addWidget(m_horizontalRuler->tabChooser(), 0, 0);
0089     gridLayout->addWidget(m_horizontalRuler, 0, 1);
0090     gridLayout->addWidget(m_verticalRuler, 1, 0);
0091     gridLayout->addWidget(canvasController, 1, 1);
0092 
0093     new KoRulerController(m_horizontalRuler, m_canvas->resourceManager());
0094 
0095     connect(m_view->kwdocument(), SIGNAL(unitChanged(KoUnit)), m_horizontalRuler, SLOT(setUnit(KoUnit)));
0096     connect(m_view->kwdocument(), SIGNAL(unitChanged(KoUnit)), m_verticalRuler, SLOT(setUnit(KoUnit)));
0097     connect(m_view->kwdocument(), SIGNAL(pageSetupChanged()), this, SLOT(pageSetupChanged()));
0098 
0099     connect(m_canvasController->proxyObject, SIGNAL(canvasOffsetXChanged(int)), m_horizontalRuler, SLOT(setOffset(int)));
0100     connect(m_canvasController->proxyObject, SIGNAL(canvasOffsetYChanged(int)), m_verticalRuler, SLOT(setOffset(int)));
0101     connect(m_canvasController->proxyObject, SIGNAL(canvasOffsetYChanged(int)), parent, SLOT(offsetInDocumentMoved(int)));
0102     connect(m_canvasController->proxyObject, SIGNAL(canvasMousePositionChanged(QPoint)), this, SLOT(updateMousePos(QPoint)));
0103     connect(m_canvasController->proxyObject, SIGNAL(moveDocumentOffset(QPoint)), m_canvas, SLOT(setDocumentOffset(QPoint)));
0104 
0105     connect(m_canvas->shapeManager()->selection(), SIGNAL(selectionChanged()), this, SLOT(shapeSelectionChanged()));
0106 
0107     m_verticalRuler->createGuideToolConnection(m_canvas);
0108     m_horizontalRuler->createGuideToolConnection(m_canvas);
0109 
0110     pageSetupChanged();
0111 
0112     QTimer::singleShot(0, this, SLOT(setupUnitActions()));
0113 }
0114 
0115 KWGui::~KWGui()
0116 {
0117 }
0118 
0119 int KWGui::visibleWidth() const
0120 {
0121     return m_canvasController->visibleWidth();
0122 }
0123 
0124 int KWGui::visibleHeight() const
0125 {
0126     return m_canvasController->visibleHeight();
0127 }
0128 
0129 QSize KWGui::viewportSize() const
0130 {
0131     return m_canvasController->viewportSize();
0132 }
0133 
0134 
0135 bool KWGui::horizontalScrollBarVisible()
0136 {
0137     return static_cast<KoCanvasControllerWidget*>(m_canvasController)->horizontalScrollBar() &&
0138            static_cast<KoCanvasControllerWidget*>(m_canvasController)->horizontalScrollBar()->isVisible();
0139 }
0140 
0141 void KWGui::pageSetupChanged()
0142 {
0143     const KWPageManager *pm = m_view->kwdocument()->pageManager();
0144     const KWPage firstPage = pm->begin();
0145     const KWPage lastPage = pm->last();
0146     int height = 0;
0147     if (lastPage.isValid())
0148         height = lastPage.offsetInDocument() + lastPage.height();
0149     m_verticalRuler->setRulerLength(height);
0150     updateRulers();
0151     int width = 0;
0152     if (firstPage.isValid())
0153         width = firstPage.width();
0154     m_horizontalRuler->setRulerLength(width);
0155     m_horizontalRuler->setActiveRange(0, width);
0156     m_verticalRuler->setActiveRange(0, height);
0157     updateRulers();
0158 }
0159 
0160 void KWGui::updateMousePos(const QPoint &point)
0161 {
0162     QPoint canvasOffset(m_canvasController->canvasOffsetX(), m_canvasController->canvasOffsetY());
0163     // the offset is positive it the canvas is shown fully visible
0164     canvasOffset.setX(canvasOffset.x() < 0 ? canvasOffset.x() : 0);
0165     canvasOffset.setY(canvasOffset.y() < 0 ? canvasOffset.y() : 0);
0166     QPoint viewPos = point - canvasOffset;
0167     m_horizontalRuler->updateMouseCoordinate(viewPos.x());
0168     m_verticalRuler->updateMouseCoordinate(viewPos.y());
0169 }
0170 
0171 void KWGui::updateRulers() const
0172 {
0173     m_verticalRuler->setVisible(m_view->kwdocument()->config().viewRulers());
0174     m_horizontalRuler->setVisible(m_view->kwdocument()->config().viewRulers());
0175 }
0176 
0177 void KWGui::shapeSelectionChanged()
0178 {
0179 }
0180 
0181 void KWGui::setupUnitActions()
0182 {
0183     QList<QAction*> unitActions = m_view->createChangeUnitActions();
0184     QAction *separator = new QAction(this);
0185     separator->setSeparator(true);
0186     unitActions.append(separator);
0187     unitActions.append(m_view->actionCollection()->action("format_page"));
0188     m_horizontalRuler->setPopupActionList(unitActions);
0189 }
0190 
0191 
0192 void KWGui::mouseMoveEvent(QMouseEvent *e)
0193 {
0194     m_view->viewMouseMoveEvent(e);
0195 }