Subversion Repositories Scribus

Rev

Rev 13809 | Go to most recent revision | Details | 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.,                                       *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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);
35
        refineSetup();
36
        activate(false);
37
        PageItemSettersManager::registerSetter(this);
38
}
39
 
40
void PageItemPositionSetter::refineSetup()
41
{
42
        keepFrameWHRatioButton->setCheckable( true );
43
        keepFrameWHRatioButton->setAutoRaise( true );
44
        keepFrameWHRatioButton->setMaximumSize( QSize( 15, 32767 ) );
45
        keepFrameWHRatioButton->setChecked(true);
46
}
47
 
48
QString PageItemPositionSetter::group() const
49
{
50
        return QString("XYZ");
51
}
52
 
53
PageItemSetterBase * PageItemPositionSetter::clone()
54
{
55
        return new PageItemPositionSetter(0);
56
}
57
 
58
void PageItemPositionSetter::changeItem(Selection * sel)
59
{
60
        removeConnections();
61
 
62
        m_select = sel;
63
        if(!m_select || (m_select->count() == 0))
64
        {
65
                activate(false);
66
                xposSpinBox->setValue(0);
67
                yposSpinBox->setValue(0);
68
                widthSpinBox->setValue(0);
69
                heightSpinBox->setValue(0);
70
                return;
71
        }
72
        activate(true);
73
        PageItem * refItem = m_select->itemAt(0);
74
        double unitRatio = refItem->m_Doc->unitRatio();
75
        double maxXYWHVal = 0xFFFFFF * unitRatio;
76
        double minXYVal = -0xFFFFFF * unitRatio;
77
        int unitIndex = refItem->m_Doc->unitIndex();
78
        int precision = unitGetPrecisionFromIndex(unitIndex);
79
        xposSpinBox->setValues( minXYVal, maxXYWHVal, precision, minXYVal);
80
        yposSpinBox->setValues( minXYVal, maxXYWHVal, precision, minXYVal);
81
        widthSpinBox->setValues(unitRatio, maxXYWHVal, precision, unitRatio);
82
        heightSpinBox->setValues(unitRatio, maxXYWHVal, precision, unitRatio);
83
        xposSpinBox->setNewUnit(unitIndex);
84
        yposSpinBox->setNewUnit(unitIndex);
85
        widthSpinBox->setNewUnit(unitIndex);
86
        heightSpinBox->setNewUnit(unitIndex);
87
        rotmode = refItem->m_Doc->RotMode();
88
        updateValues();
89
}
90
 
91
void PageItemPositionSetter::createConnections()
92
{
93
        if(hasConnections)
94
                return;
95
        connectedItem = m_select->itemAt(0);
96
        connect(connectedItem, SIGNAL(position(double,double)), this, SLOT(captureChanges(double,double)));
97
        connect(connectedItem, SIGNAL(widthAndHeight(double,double)), this, SLOT(captureChanges(double,double)));
98
        connect(xposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modXpos(double)));
99
        connect(yposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modYpos(double)));
100
        connect(heightSpinBox,SIGNAL(valueChanged(double)), this, SLOT(modHeight(double)));
101
        connect(widthSpinBox, SIGNAL(valueChanged(double)),this, SLOT(modWidth(double)));
102
        hasConnections = true;
103
        activate(true);
104
}
105
 
106
void PageItemPositionSetter::removeConnections()
107
{
108
        if(!hasConnections)
109
                return;
110
        disconnect(connectedItem, SIGNAL(position(double,double)), this, SLOT(captureChanges(double,double)));
111
        disconnect(connectedItem, SIGNAL(widthAndHeight(double,double)), this, SLOT(captureChanges(double,double)));
112
        disconnect(xposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modXpos(double)));
113
        disconnect(yposSpinBox, SIGNAL(valueChanged(double)), this, SLOT(modYpos(double)));
114
        disconnect(heightSpinBox,SIGNAL(valueChanged(double)), this, SLOT(modHeight(double)));
115
        disconnect(widthSpinBox, SIGNAL(valueChanged(double)),this, SLOT(modWidth(double)));
116
        hasConnections = false;
117
        connectedItem = 0;
118
        activate(false);
119
}
120
 
121
void PageItemPositionSetter::updateValues()
122
{
123
        if(!m_select || (m_select->count() == 0))
124
                return;
125
        removeConnections();
126
 
127
        PageItem * Item =  m_select->itemAt(0);
128
        QRectF r(Item->xPos(), Item->yPos(), Item->width(), Item->height());
129
        if(m_select->isMultipleSelection())
130
                        r = m_select->getGroupRect();
131
        double baseAdjustX = 0;
132
        double baseAdjustY = 0;
133
        adjustBase(baseAdjustX, baseAdjustY, r);
134
 
135
        qDebug()<<"U"<<r<<baseAdjustX<<baseAdjustY;
136
 
137
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
138
        double displayX = (r.x() - doc->currentPage()->xOffset() + baseAdjustX) * doc->unitRatio();
139
        double displayY = (r.y() - doc->currentPage()->yOffset() + baseAdjustY) * doc->unitRatio();
140
        double displayW = r.width() *  doc->unitRatio();
141
        double displayH = r.height() * doc->unitRatio();
142
 
143
        xposSpinBox->setValue(displayX);
144
        yposSpinBox->setValue(displayY);
145
        widthSpinBox->setValue(displayW);
146
        heightSpinBox->setValue(displayH);
147
 
148
        createConnections();
149
}
150
 
151
void PageItemPositionSetter::captureChanges(double, double)
152
{
153
        updateValues();
154
}
155
 
156
void PageItemPositionSetter::modXpos(double val)
157
{
158
        if(!m_select || (m_select->count() == 0))
159
                return;
160
        // first check if we can move at all, though it should be prevented elsewhere
161
        foreach(PageItem* pit, m_select->selectionList())
162
        {
163
                if(pit->locked())
164
                        return;
165
        }
166
        removeConnections();
167
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
168
        QRectF oldVisualRect = m_select->getVisualGroupRect();
169
        QRectF oldRect = m_select->getGroupRect();
170
        double baseAdjustX = 0;
171
        double baseAdjustY = 0;
172
        adjustBase(baseAdjustX, baseAdjustY, m_select->getGroupRect());
173
        double xoffset = doc->currentPage()->xOffset();
174
        double newX = (xposSpinBox->value() /  doc->unitRatio())  - baseAdjustX;
175
        double shiftH = newX - (oldRect.x() - xoffset) ;
176
        foreach(PageItem* pit, m_select->selectionList())
177
        {
178
                pit->moveBy(shiftH,0);
179
        }
180
        updateCanvas(oldVisualRect.unite(m_select->getVisualGroupRect()));
181
        createConnections();
182
}
183
 
184
void PageItemPositionSetter::modYpos(double val)
185
{
186
        if(!m_select || (m_select->count() == 0))
187
                return;
188
        // first check if we can move at all, though it should be prevented elsewhere
189
        foreach(PageItem* pit, m_select->selectionList())
190
        {
191
                if(pit->locked())
192
                        return;
193
        }
194
        removeConnections();
195
        ScribusDoc * doc = m_select->itemAt(0)->m_Doc;
196
        QRectF oldVisualRect = m_select->getVisualGroupRect();
197
        QRectF oldRect = m_select->getGroupRect();
198
        double baseAdjustX = 0;
199
        double baseAdjustY = 0;
200
        adjustBase(baseAdjustX, baseAdjustY, m_select->getGroupRect());
201
        double yoffset = doc->currentPage()->yOffset();
202
        double newY = (yposSpinBox->value() /  doc->unitRatio()) - baseAdjustY;
203
        double shiftV = newY - (oldRect.y() - yoffset);
204
        foreach(PageItem* pit, m_select->selectionList())
205
        {
206
                pit->moveBy(0, shiftV);
207
        }
208
        updateCanvas(oldVisualRect.unite(m_select->getVisualGroupRect()));
209
        createConnections();
210
}
211
 
212
void PageItemPositionSetter::modWidth(double val)
213
{
214
        if(!m_select || (m_select->count() == 0))
215
                return;
216
        foreach(PageItem* pit, m_select->selectionList())
217
        {
218
                if(pit->locked() || pit->sizeLocked())
219
                        return;
220
        }
221
        removeConnections();
222
        double unitRatio = m_select->itemAt(0)->m_Doc->unitRatio();
223
        QRectF oldRect = m_select->getVisualGroupRect();
224
        QRectF r = m_select->getGroupRect();
225
        double scaleFactor = (widthSpinBox->value() / unitRatio) / r.width() ;
226
        foreach(PageItem* pit, m_select->selectionList())
227
        {
228
                double newW = pit->width() * scaleFactor;
229
                pit->setWidth(newW);
230
                if(keepFrameWHRatioButton->isChecked())
231
                {
232
                        double newH = pit->height() * scaleFactor;
233
                        pit->setHeight(newH);
234
                        heightSpinBox->setValue(newH * pit->m_Doc->unitRatio());
235
                }
236
        }
237
        updateCanvas(oldRect.unite(m_select->getVisualGroupRect()));
238
        createConnections();
239
}
240
 
241
void PageItemPositionSetter::modHeight(double val)
242
{
243
        if(!m_select || (m_select->count() == 0))
244
                return;
245
        foreach(PageItem* pit, m_select->selectionList())
246
        {
247
                if(pit->locked() || pit->sizeLocked())
248
                        return;
249
        }
250
        removeConnections();
251
        double unitRatio = m_select->itemAt(0)->m_Doc->unitRatio();
252
        QRectF oldRect = m_select->getVisualGroupRect();
253
        QRectF r = m_select->getGroupRect();
254
        double scaleFactor =  (heightSpinBox->value() / unitRatio) / r.height() ;
255
        foreach(PageItem* pit, m_select->selectionList())
256
        {
257
                double newH = pit->height() * scaleFactor;
258
                pit->setHeight(newH);
259
                if(keepFrameWHRatioButton->isChecked())
260
                {
261
                        double newW = pit->width() * scaleFactor;
262
                        pit->setWidth(newW);
263
                        widthSpinBox->setValue(newW * pit->m_Doc->unitRatio());
264
                }
265
        }
266
        updateCanvas(oldRect.unite(m_select->getVisualGroupRect()));
267
        createConnections();
268
}
269
 
270
void PageItemPositionSetter::updateCanvas(QRectF rect)
271
{
272
        //      qDebug()<<"updateCanvas"<<rect;
273
        //      m_Item->setRedrawBounding();
274
 
275
        m_select->itemAt(0)->m_Doc->regionsChanged()->update(rect);
276
}
277
 
278
void PageItemPositionSetter::adjustBase(double &baseAdjustX , double &baseAdjustY, QRectF bb)
279
{
280
        if(rotmode == 1)
281
                baseAdjustX = bb.width();
282
        else if(rotmode == 2)
283
        {
284
                baseAdjustX =  bb.width() / 2.0;
285
                baseAdjustY =  bb.height() / 2.0;
286
        }
287
        else if(rotmode == 3)
288
                baseAdjustY = bb.height();
289
        else if(rotmode == 4)
290
        {
291
                baseAdjustX =  bb.width();
292
                baseAdjustY =  bb.height();
293
        }
294
 
295
}