Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_document_aware_spin_box_unit_manager.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include "KisPart.h"
10#include "KisMainWindow.h"
11#include "KisView.h"
12#include "KisDocument.h"
13#include "kis_types.h"
14#include "kis_image.h"
16#include "kis_time_span.h"
17
18
23
30
38
41{
42 if (pPixDir == PIX_DIR_Y) {
44 } else {
46 }
47
48 grantDocumentRelativeUnits(); //the purpose of this class is to manage document relative units.
49}
50
51
53{
54 QString symbol = psymbol;
55
56 if (symbol == "%") { //percent can be seen as vw or vh depending of the reference side in the image.
57 if (pixDir == PIX_DIR_X) {
58 symbol = "vw";
59 } else {
60 symbol = "vh";
61 }
62 }
63
65
66 if (factor > 0) {
67 //no errors occurred at a lower level, so the conversion factor has been get.
68 return factor;
69 }
70
71 factor = 1; //fall back to something natural in case document is unreachable (1 px = 1 pt = 1vw = 1vh). So a virtual document of 100x100 with a resolution of 1.
72
73 if (!KisPart::instance()->currentMainwindow()) {
74 return factor;
75 }
76
78
79 if (view == nullptr) {
80 return factor;
81 }
82
83 KisDocument* doc = view->document();
84
85 if (doc == nullptr) {
86 return factor;
87 }
88
89 KisImage* img = doc->image().data();
90
91 if (img == nullptr) {
92 return factor;
93 }
94
95 qreal resX = img->xRes();
96 qreal resY = img->yRes();
97 qreal sizeX = img->width();
98 qreal sizeY = img->height();
99
100 switch (dim) {
101
102 case LENGTH:
103 if (symbol == "px") {
104
105 if (pixDir == PIX_DIR_X) {
106 factor = resX;
107 } else {
108 factor = resY;
109 }
110 } else if (symbol == "vw") {
111 qreal docWidth = sizeX/resX;
112
113 factor = 100.0/docWidth; //1 vw is 1% of document width, 1 vw in point is docWidth/100 so 1 point in vw is the inverse.
114 } else if (symbol == "vh") {
115 qreal docHeight = sizeY/resY;
116
117 factor = 100.0/docHeight;
118 }
119 break;
120
121 case IMLENGTH:
122
123 if (symbol == "vw") {
124 factor = 100.0/sizeX; //1 vw is 1% of document width, 1 vw in pixel is sizeX/100 so 1 pixel in vw is the inverse.
125
126 } else if (symbol == "vh") {
127 factor = 100.0/sizeY;
128 }
129 break;
130
131 case TIME:
132 {
133 if (symbol == "s") {
134 qreal fps = img->animationInterface()->framerate();
135
136 factor = 1/fps;
137 } else if (symbol == "%") {
138 const KisTimeSpan & time_range = img->animationInterface()->documentPlaybackRange();
139 qreal n_frame = time_range.end() - time_range.start();
140
141 factor = 100/n_frame;
142 }
143 }
144 break;
145
146 default:
147 break;
148
149 }
150
151 return factor;
152}
153
155{
156 if (dim == TIME && symbol == "%") {
157 KisImage* img = KisPart::instance()->currentMainwindow()->activeView()->document()->image().data();
158 const KisTimeSpan & time_range = img->animationInterface()->documentPlaybackRange();
159 qreal n_frame = time_range.end() - time_range.start();
160
161 return -time_range.start()*100.0/n_frame;
162 }
163
165}
166
167
169
170 if (unitDim == IMLENGTH || unitDim == LENGTH) {
171 return true;
172 }
173
175}
KisSpinBoxUnitManager * buildUnitManager(QObject *parent) override
The KisDocumentAwareSpinBoxUnitManager class is a KisSpinBoxUnitManager that is able to connect to th...
qreal getConversionConstant(int dim, QString symbol) const override
static void setDocumentAwarenessToExistingUnitSpinBox(KisDoubleParseUnitSpinBox *spinBox, bool setUnitFromOutsideToggle=false)
configure a KisDocumentAwareSpinBoxUnitManager for the given spinbox (make the manager a child of the...
KisDocumentAwareSpinBoxUnitManager(QObject *parent=0, int pPixDir=PIX_DIR_X)
qreal getConversionFactor(int dim, QString psymbol) const override
static KisDoubleParseUnitSpinBox * createUnitSpinBoxWithDocumentAwareness(QWidget *parent=0)
create a unitSpinBox that is already document aware.
KisImageSP image
The KisDoubleParseUnitSpinBox class is an evolution of the.
void setUnitManager(KisSpinBoxUnitManager *unitManager)
double xRes() const
QPointer< KisView > activeView
static KisPart * instance()
Definition KisPart.cpp:131
KisMainWindow * currentMainwindow() const
Definition KisPart.cpp:483
The KisSpinBoxUnitManager class is an abstract interface for the unitspinboxes classes to manage diff...
virtual qreal getConversionConstant(int dim, QString symbol) const
some units conversions are done via an affine transform, not just a linear transform....
virtual qreal getConversionFactor(int dim, QString symbol) const
gets the conversion factor of a managed unit, or -1 in case of error. This method is the one that nee...
KisSpinBoxUnitManager::UnitDimension dim
void grantDocumentRelativeUnits()
calling this method gives access to document relative units. Only subclasses that manage those units ...
virtual bool hasPercent(int unitDim) const
indicate if the unit manager has some kind of way of using a percent unit, used by the main class to ...
int end() const
QPointer< KisDocument > document
Definition KisView.cpp:121