overviewdocker_dock.cpp 4.88 KB
Newer Older
Halla Rempt's avatar
Halla Rempt committed
1
/*
2
 *  SPDX-FileCopyrightText: 2009 Cyrille Berger <cberger@cberger.net>
Halla Rempt's avatar
Halla Rempt committed
3
 *
Samuel Gaist's avatar
Samuel Gaist committed
4
 *  SPDX-License-Identifier: LGPL-2.0-or-later
Halla Rempt's avatar
Halla Rempt committed
5 6 7
 */

#include "overviewdocker_dock.h"
8
#include "overviewwidget.h"
Halla Rempt's avatar
Halla Rempt committed
9 10

#include <QLabel>
11
#include <QVBoxLayout>
12 13
#include <QHBoxLayout>
#include <QToolButton>
Halla Rempt's avatar
Halla Rempt committed
14
#include <QStatusBar>
Halla Rempt's avatar
Halla Rempt committed
15

16 17
#include <KisAngleSelector.h>
#include <klocalizedstring.h>
Halla Rempt's avatar
Halla Rempt committed
18
#include "kis_canvas2.h"
19
#include <KisViewManager.h>
20 21
#include <kactioncollection.h>
#include <kis_action.h>
22
#include <kis_zoom_manager.h>
Halla Rempt's avatar
Halla Rempt committed
23 24
#include "kis_image.h"
#include "kis_paint_device.h"
25
#include "kis_signal_compressor.h"
26 27
#include "kis_canvas_controller.h"
#include "kis_icon_utils.h"
28
#include "kis_signals_blocker.h"
Halla Rempt's avatar
Halla Rempt committed
29 30 31

OverviewDockerDock::OverviewDockerDock( )
    : QDockWidget(i18n("Overview"))
32
    , m_zoomSlider(nullptr)
33
    , m_rotateAngleSelector(nullptr)
34 35
    , m_mirrorCanvas(nullptr)
    , m_canvas(nullptr)
Halla Rempt's avatar
Halla Rempt committed
36 37
{
    QWidget *page = new QWidget(this);
38
    m_layout = new QVBoxLayout(page);
39 40 41 42
    m_layout->setSpacing(2);
    m_bottomLayout = new QVBoxLayout();
    m_bottomLayout->setContentsMargins(2, 2, 2, 2);
    m_bottomLayout->setSpacing(2);
43
    m_horizontalLayout = new QHBoxLayout();
Halla Rempt's avatar
Halla Rempt committed
44

45 46
    m_overviewWidget = new OverviewWidget(this);
    m_overviewWidget->setMinimumHeight(50);
47 48
    m_overviewWidget->setBackgroundRole(QPalette::AlternateBase);
    m_overviewWidget->setAutoFillBackground(true); // paints background role before paint()
Halla Rempt's avatar
Halla Rempt committed
49

50
    m_layout->addWidget(m_overviewWidget, 1);
51
    m_layout->addLayout(m_bottomLayout);
Halla Rempt's avatar
Halla Rempt committed
52 53 54 55 56 57

    setWidget(page);
}

void OverviewDockerDock::setCanvas(KoCanvasBase * canvas)
{
58 59 60
    if(m_canvas == canvas)
        return;

61
    setEnabled(canvas != nullptr);
62

Halla Rempt's avatar
Halla Rempt committed
63 64
    if (m_canvas) {
        m_canvas->disconnectCanvasObserver(this);
65
        m_canvas->image()->disconnect(this);
Halla Rempt's avatar
Halla Rempt committed
66
    }
67

68
    if (m_zoomSlider) {
69
        m_bottomLayout->removeWidget(m_zoomSlider);
70
        delete m_zoomSlider;
71 72 73
        m_zoomSlider = nullptr;
    }

74 75 76 77
    if (m_rotateAngleSelector) {
        m_horizontalLayout->removeWidget(m_rotateAngleSelector);
        delete m_rotateAngleSelector;
        m_rotateAngleSelector = nullptr;
78 79 80 81 82 83
    }

    if (m_mirrorCanvas) {
        m_horizontalLayout->removeWidget(m_mirrorCanvas);
        delete m_mirrorCanvas;
        m_mirrorCanvas = nullptr;
84
    }
Halla Rempt's avatar
Halla Rempt committed
85

Halla Rempt's avatar
Halla Rempt committed
86 87 88 89 90
    // Delete the stretch
    if (m_horizontalLayout->count() && m_horizontalLayout->itemAt(0)->spacerItem()) {
        delete m_horizontalLayout->takeAt(0);
    }

91
    m_bottomLayout->removeItem(m_horizontalLayout);
92

Halla Rempt's avatar
Halla Rempt committed
93 94
    m_canvas = dynamic_cast<KisCanvas2*>(canvas);

95
    m_overviewWidget->setCanvas(canvas);
96
    if (m_canvas && m_canvas->viewManager() && m_canvas->viewManager()->zoomController() && m_canvas->viewManager()->zoomController()->zoomAction()) {
97
        m_zoomSlider = m_canvas->viewManager()->zoomController()->zoomAction()->createWidget(m_canvas->imageView()->KisView::statusBar());
98 99 100 101 102 103 104 105
        m_bottomLayout->addWidget(m_zoomSlider);
        
        m_rotateAngleSelector = new KisAngleSelector();
        m_rotateAngleSelector->setRange(-179.99, 180.0);
        m_rotateAngleSelector->setAngle(m_canvas->rotationAngle());
        m_rotateAngleSelector->setIncreasingDirection(KisAngleGauge::IncreasingDirection_Clockwise);
        m_rotateAngleSelector->setFlipOptionsMode(KisAngleSelector::FlipOptionsMode_ContextMenu);
        connect(m_rotateAngleSelector, SIGNAL(angleChanged(qreal)), this, SLOT(rotateCanvasView(qreal)), Qt::UniqueConnection);
Halla Rempt's avatar
Halla Rempt committed
106 107
        connect(m_canvas->canvasController()->proxyObject, SIGNAL(canvasOffsetXChanged(int)), this, SLOT(updateSlider()));

108 109 110 111 112 113 114
        m_mirrorCanvas = new QToolButton();
        QList<QAction *> actions = m_canvas->viewManager()->actionCollection()->actions();
        Q_FOREACH(QAction* action, actions) {
            if (action->objectName()=="mirror_canvas") {
                m_mirrorCanvas->setDefaultAction(action);
            }
        }
115 116 117
        
        m_horizontalLayout->addWidget(m_rotateAngleSelector);
        m_horizontalLayout->addStretch();
118
        m_horizontalLayout->addWidget(m_mirrorCanvas);
119 120
        m_bottomLayout->addLayout(m_horizontalLayout);
        m_rotateAngleSelector->setVisible(true);
121
    }
Halla Rempt's avatar
Halla Rempt committed
122 123
}

124
void OverviewDockerDock::unsetCanvas()
Halla Rempt's avatar
Halla Rempt committed
125
{
126
    setEnabled(false);
127
    m_canvas = nullptr;
128
    m_overviewWidget->unsetCanvas();
Halla Rempt's avatar
Halla Rempt committed
129 130
}

131 132
void OverviewDockerDock::rotateCanvasView(qreal rotation)
{
133
    if (!m_canvas) return;
134 135 136 137 138 139 140 141 142
    KisCanvasController *canvasController =
            dynamic_cast<KisCanvasController*>(m_canvas->viewManager()->canvasBase()->canvasController());
    if (canvasController) {
        canvasController->rotateCanvas(rotation-m_canvas->rotationAngle());
    }
}

void OverviewDockerDock::updateSlider()
{
143
    if (!m_canvas) return;
144
    KisSignalsBlocker l(m_rotateAngleSelector);
145

146 147 148 149 150 151
    qreal rotation = m_canvas->rotationAngle();
    if (rotation > 180) {
        rotation = rotation - 360;
    } else if (rotation < -180) {
        rotation = rotation + 360;
    }
152
    m_rotateAngleSelector->setAngle(rotation);
153 154
}

Halla Rempt's avatar
Halla Rempt committed
155