Subversion Repositories Scribus

Rev

Rev 13809 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13808 pierre 1
/***************************************************************************
2
 *   Copyright (C) 2009 by Pierre Marchand                                 *
3
 *   pierre@oep-h.com                                                      *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 *   (at your option) any later version.                                   *
9
 *                                                                         *
10
 *   This program is distributed in the hope that it will be useful,       *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   GNU General Public License for more details.                          *
14
 *                                                                         *
15
 *   You should have received a copy of the GNU General Public License     *
16
 *   along with this program; if not, write to the                         *
17
 *   Free Software Foundation, Inc.,                                       *
18122 mrdocs 18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
13808 pierre 19
 ***************************************************************************/
20
 
21
#include "pageitempositionsetter.h"
22
#include "pageitemsettersmanager.h"
23
 
24
#include "pageitem.h"
25
#include "scribusdoc.h"
26
#include "units.h"
27
#include "selection.h"
28
 
29
#include <QDebug>
30
 
31
PageItemPositionSetter::PageItemPositionSetter(QWidget * parent)
32
                :PageItemSetterBase(parent), m_select(0), hasConnections(false)
33
{
34
        setupUi(this);
13809 pierre 35
        setObjectName("XYZ.geometry.position");
13808 pierre 36
        refineSetup();
37
        activate(false);
38
        PageItemSettersManager::registerSetter(this);
39
}
40
 
41
void PageItemPositionSetter::refineSetup()
42
{
43
        keepFrameWHRatioButton->setCheckable( true );
44
        keepFrameWHRatioButton->setAutoRaise( true );
45
        keepFrameWHRatioButton->setMaximumSize( QSize( 15, 32767 ) );
46
        keepFrameWHRatioButton->setChecked(true);
47
}
48
 
49
PageItemSetterBase * PageItemPositionSetter::clone()
50
{
51
        return new PageItemPositionSetter(0);
52
}
53
 
54
void PageItemPositionSetter::changeItem(Selection * sel)
55
{
56
        removeConnections();
57
 
58
        m_select = sel;
59
        if(!m_select || (m_select->count() == 0))
60
        {
61
                activate(false);
62
                xposSpinBox->setValue(0);
63
                yposSpinBox->setValue(0);
64
                widthSpinBox->setValue(0);
65
                heightSpinBox->setValue(0);
66
                return;
67
        }
68
        activate(true);
69
        PageItem * refItem = m_select->itemAt(0);
70
        double unitRatio = refItem->m_Doc->unitRatio();
71
        double maxXYWHVal = 0xFFFFFF * unitRatio;
72
        double minXYVal = -0xFFFFFF * unitRatio;
73
        int unitIndex = refItem->m_Doc->unitIndex();
74
        int precision = unitGetPrecisionFromIndex(unitIndex);
75
        xposSpinBox->setValues( minXYVal, maxXYWHVal, precision, minXYVal);
76
        yposSpinBox->setValues( minXYVal, maxXYWHVal, precision, minXYVal);
77
        widthSpinBox->setValues(unitRatio, maxXYWHVal, precision, unitRatio);
78
        heightSpinBox->setValues(unitRatio, maxXYWHVal, precision, unitRatio);
79
        xposSpinBox->setNewUnit(unitIndex);
80
        yposSpinBox->setNewUnit(unitIndex);
81
        widthSpinBox->setNewUnit(unitIndex);
82
        heightSpinBox->setNewUnit(unitIndex);
83
        rotmode = refItem->m_Doc->RotMode();
84
        updateValues();
85
}
86
 
87
void PageItemPositionSetter::createConnections()
88
{
89
        if(hasConnections)
90
                return;
91
        connectedItem = m_select->itemAt(0);
92
        connect(connectedItem, SIGNAL(position(double,double)), this, SLOT(captureChanges(double,double)));
93
        connect(connectedItem, SIGNAL(widthAndHeight(double,double)), this, SLOT(captureChanges(double,double)));
94
        connect(xposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modXpos(double)));
95
        connect(yposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modYpos(double)));
96
        connect(heightSpinBox,SIGNAL(valueChanged(double)), this, SLOT(modHeight(double)));
97
        connect(widthSpinBox, SIGNAL(valueChanged(double)),this, SLOT(modWidth(double)));
98
        hasConnections = true;
99
        activate(true);
100
}
101
 
102
void PageItemPositionSetter::removeConnections()
103
{
104
        if(!hasConnections)
105
                return;
106
        disconnect(connectedItem, SIGNAL(position(double,double)), this, SLOT(captureChanges(double,double)));
107
        disconnect(connectedItem, SIGNAL(widthAndHeight(double,double)), this, SLOT(captureChanges(double,double)));
108
        disconnect(xposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modXpos(double)));
109
        disconnect(yposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modYpos(double)));
110
        disconnect(heightSpinBox,SIGNAL(valueChanged(double)), this, SLOT(modHeight(double)));
111
        disconnect(widthSpinBox, SIGNAL(valueChanged(double)),this, SLOT(modWidth(double)));
112
        hasConnections = false;
113
        connectedItem = 0;
114
        activate(false);
115
}
116
 
117
void PageItemPositionSetter::updateValues()
118
{
119
        if(!m_select || (m_select->count() == 0))
120
                return;
121
        removeConnections();
122
 
123
        PageItem * Item =  m_select->itemAt(0);
124
        QRectF r(Item->xPos(), Item->yPos(), Item->width(), Item->height());
125
        if(m_select->isMultipleSelection())
126
                        r = m_select->getGroupRect();
127
        double baseAdjustX = 0;
128
        double baseAdjustY = 0;
129
        adjustBase(baseAdjustX, baseAdjustY, r);
130
 
131
        qDebug()<<"U"<<r<<baseAdjustX<<baseAdjustY;
132
 
133
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
134
        double displayX = (r.x() - doc->currentPage()->xOffset() + baseAdjustX) * doc->unitRatio();
135
        double displayY = (r.y() - doc->currentPage()->yOffset() + baseAdjustY) * doc->unitRatio();
136
        double displayW = r.width() *  doc->unitRatio();
137
        double displayH = r.height() * doc->unitRatio();
138
 
139
        xposSpinBox->setValue(displayX);
140
        yposSpinBox->setValue(displayY);
141
        widthSpinBox->setValue(displayW);
142
        heightSpinBox->setValue(displayH);
143
 
144
        createConnections();
145
}
146
 
147
void PageItemPositionSetter::captureChanges(double, double)
148
{
149
        updateValues();
150
}
151
 
152
void PageItemPositionSetter::modXpos(double val)
153
{
154
        if(!m_select || (m_select->count() == 0))
155
                return;
156
        // first check if we can move at all, though it should be prevented elsewhere
157
        foreach(PageItem* pit, m_select->selectionList())
158
        {
159
                if(pit->locked())
160
                        return;
161
        }
162
        removeConnections();
163
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
164
        QRectF oldVisualRect = m_select->getVisualGroupRect();
165
        QRectF oldRect = m_select->getGroupRect();
166
        double baseAdjustX = 0;
167
        double baseAdjustY = 0;
168
        adjustBase(baseAdjustX, baseAdjustY, m_select->getGroupRect());
169
        double xoffset = doc->currentPage()->xOffset();
170
        double newX = (xposSpinBox->value() /  doc->unitRatio())  - baseAdjustX;
171
        double shiftH = newX - (oldRect.x() - xoffset) ;
172
        foreach(PageItem* pit, m_select->selectionList())
173
        {
174
                pit->moveBy(shiftH,0);
175
        }
176
        updateCanvas(oldVisualRect.unite(m_select->getVisualGroupRect()));
177
        createConnections();
178
}
179
 
180
void PageItemPositionSetter::modYpos(double val)
181
{
182
        if(!m_select || (m_select->count() == 0))
183
                return;
184
        // first check if we can move at all, though it should be prevented elsewhere
185
        foreach(PageItem* pit, m_select->selectionList())
186
        {
187
                if(pit->locked())
188
                        return;
189
        }
190
        removeConnections();
191
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
192
        QRectF oldVisualRect = m_select->getVisualGroupRect();
193
        QRectF oldRect = m_select->getGroupRect();
194
        double baseAdjustX = 0;
195
        double baseAdjustY = 0;
196
        adjustBase(baseAdjustX, baseAdjustY, m_select->getGroupRect());
197
        double yoffset = doc->currentPage()->yOffset();
198
        double newY = (yposSpinBox->value() /  doc->unitRatio()) - baseAdjustY;
199
        double shiftV = newY - (oldRect.y() - yoffset);
200
        foreach(PageItem* pit, m_select->selectionList())
201
        {
202
                pit->moveBy(0, shiftV);
203
        }
204
        updateCanvas(oldVisualRect.unite(m_select->getVisualGroupRect()));
205
        createConnections();
206
}
207
 
208
void PageItemPositionSetter::modWidth(double val)
209
{
210
        if(!m_select || (m_select->count() == 0))
211
                return;
212
        foreach(PageItem* pit, m_select->selectionList())
213
        {
214
                if(pit->locked() || pit->sizeLocked())
215
                        return;
216
        }
217
        removeConnections();
218
        double unitRatio = m_select->itemAt(0)->m_Doc->unitRatio();
219
        QRectF oldRect = m_select->getVisualGroupRect();
220
        QRectF r = m_select->getGroupRect();
221
        double scaleFactor = (widthSpinBox->value() / unitRatio) / r.width() ;
222
        foreach(PageItem* pit, m_select->selectionList())
223
        {
224
                double newW = pit->width() * scaleFactor;
225
                pit->setWidth(newW);
226
                if(keepFrameWHRatioButton->isChecked())
227
                {
228
                        double newH = pit->height() * scaleFactor;
229
                        pit->setHeight(newH);
230
                        heightSpinBox->setValue(newH * pit->m_Doc->unitRatio());
231
                }
232
        }
233
        updateCanvas(oldRect.unite(m_select->getVisualGroupRect()));
234
        createConnections();
235
}
236
 
237
void PageItemPositionSetter::modHeight(double val)
238
{
239
        if(!m_select || (m_select->count() == 0))
240
                return;
241
        foreach(PageItem* pit, m_select->selectionList())
242
        {
243
                if(pit->locked() || pit->sizeLocked())
244
                        return;
245
        }
246
        removeConnections();
247
        double unitRatio = m_select->itemAt(0)->m_Doc->unitRatio();
248
        QRectF oldRect = m_select->getVisualGroupRect();
249
        QRectF r = m_select->getGroupRect();
250
        double scaleFactor =  (heightSpinBox->value() / unitRatio) / r.height() ;
251
        foreach(PageItem* pit, m_select->selectionList())
252
        {
253
                double newH = pit->height() * scaleFactor;
254
                pit->setHeight(newH);
255
                if(keepFrameWHRatioButton->isChecked())
256
                {
257
                        double newW = pit->width() * scaleFactor;
258
                        pit->setWidth(newW);
259
                        widthSpinBox->setValue(newW * pit->m_Doc->unitRatio());
260
                }
261
        }
262
        updateCanvas(oldRect.unite(m_select->getVisualGroupRect()));
263
        createConnections();
264
}
265
 
266
void PageItemPositionSetter::updateCanvas(QRectF rect)
267
{
268
        //      qDebug()<<"updateCanvas"<<rect;
269
        //      m_Item->setRedrawBounding();
270
 
271
        m_select->itemAt(0)->m_Doc->regionsChanged()->update(rect);
272
}
273
 
274
void PageItemPositionSetter::adjustBase(double &baseAdjustX , double &baseAdjustY, QRectF bb)
275
{
276
        if(rotmode == 1)
277
                baseAdjustX = bb.width();
278
        else if(rotmode == 2)
279
        {
280
                baseAdjustX =  bb.width() / 2.0;
281
                baseAdjustY =  bb.height() / 2.0;
282
        }
283
        else if(rotmode == 3)
284
                baseAdjustY = bb.height();
285
        else if(rotmode == 4)
286
        {
287
                baseAdjustX =  bb.width();
288
                baseAdjustY =  bb.height();
289
        }
290
 
291
}