~ubuntu-branches/ubuntu/maverick/scribus-ng/maverick-backports

« back to all changes in this revision

Viewing changes to scribus/dasheditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Oleksandr Moskalenko
  • Date: 2009-02-09 09:25:18 UTC
  • mfrom: (5.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090209092518-iqsxmh3pjspgrdyd
Tags: 1.3.5.dfsg~svn20090208-2
debian/control: Use "type-handling -n arm,armel,armeb any" to generate the
list of architectures to build on.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
 
3
to the COPYING file provided with the program. Following this notice may exist
 
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
 
5
for which a new license (GPL+exception) is in place.
 
6
*/
 
7
/***************************************************************************
 
8
                          gradienteditor  -  description
 
9
                             -------------------
 
10
    begin                : Mit Mai 26 2004
 
11
    copyright            : (C) 2004 by Franz Schmid
 
12
    email                : Franz.Schmid@altmuehlnet.de
 
13
 ***************************************************************************/
 
14
 
 
15
/***************************************************************************
 
16
 *                                                                         *
 
17
 *   This program is free software; you can redistribute it and/or modify  *
 
18
 *   it under the terms of the GNU General Public License as published by  *
 
19
 *   the Free Software Foundation; either version 2 of the License, or     *
 
20
 *   (at your option) any later version.                                   *
 
21
 *                                                                         *
 
22
 ***************************************************************************/
 
23
 
 
24
#include "dasheditor.h"
 
25
 
 
26
#include <QApplication>
 
27
#include <QCursor>
 
28
#include <QEvent>
 
29
#include <QMouseEvent>
 
30
#include <QPaintEvent>
 
31
#include <QPainter>
 
32
#include <QPixmap>
 
33
#include <QPolygon>
 
34
#include <QToolTip>
 
35
 
 
36
#include "scpainter.h"
 
37
#include "fpoint.h"
 
38
#include "util_icon.h"
 
39
 
 
40
DashPreview::DashPreview(QWidget *pa) : QFrame(pa)
 
41
{
 
42
        setFrameShape( QFrame::Panel );
 
43
        setFrameShadow( QFrame::Sunken );
 
44
        setLineWidth( 2 );
 
45
        setMinimumSize(QSize(200, 35));
 
46
        setMaximumSize(QSize(3000, 35));
 
47
        setMouseTracking(true);
 
48
        Mpressed = false;
 
49
        outside = false;
 
50
        onlyselect = true;
 
51
        StopM.clear();
 
52
        ActStop = 0;
 
53
        DashValues.clear();
 
54
        DashValues.append(4.0);
 
55
        DashValues.append(2.0);
 
56
 
57
 
 
58
void DashPreview::paintEvent(QPaintEvent *e)
 
59
{
 
60
        if (onlyselect)
 
61
                StopM.clear();
 
62
        int pWidth = width()-20;
 
63
        QImage pixm(pWidth, 10, QImage::Format_ARGB32);
 
64
        ScPainter *p = new ScPainter(&pixm, pWidth, 10);
 
65
        p->clear(QColor(128, 128, 128));
 
66
        double startX = 0.0;
 
67
        p->setLineWidth(0);
 
68
        p->setFillMode(1);
 
69
        p->setBrush(Qt::black);
 
70
        for (int a = 0; a < DashValues.count(); a++)
 
71
        {
 
72
                if (a % 2 == 0)
 
73
                        p->setBrush(Qt::black);
 
74
                else
 
75
                        p->setBrush(Qt::white);
 
76
                double w = DashValues[a] * 10;
 
77
                p->drawRect(startX, 0, w, 10);
 
78
                startX += w;
 
79
                if (onlyselect)
 
80
                        StopM.append(startX);
 
81
        }
 
82
        p->setPen(Qt::black);
 
83
        p->setLineWidth(1);
 
84
        p->setFillMode(0);
 
85
        p->drawRect(0, 0, pWidth, 10);
 
86
        p->end();
 
87
        delete p;
 
88
        QPainter pw;
 
89
        pw.begin(this);
 
90
        pw.drawImage(10, 5, pixm);
 
91
        for (int a = 0; a < StopM.count(); ++a)
 
92
        {
 
93
                double center = StopM[a]+10;
 
94
                pw.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
 
95
                if (ActStop == a)
 
96
                {
 
97
                        emit currStep(DashValues[ActStop]);
 
98
                        pw.setBrush(Qt::red);
 
99
                }
 
100
                else
 
101
                        pw.setBrush(Qt::blue);
 
102
                QPolygon cr;
 
103
                cr.setPoints(3, qRound(center), 16, qRound(center-4), 29, qRound(center+4), 29);
 
104
                pw.drawPolygon(cr);
 
105
        }
 
106
        pw.end();
 
107
        QFrame::paintEvent(e);
 
108
        onlyselect = true;
 
109
}
 
110
 
 
111
void DashPreview::mousePressEvent(QMouseEvent *m)
 
112
{
 
113
        QRect fpo;
 
114
        m_moveTimer.start();
 
115
        Mpressed = true;
 
116
        ActStop = -1;
 
117
        m->accept();
 
118
        for (int yg = 0; yg < StopM.count(); ++yg)
 
119
        {
 
120
                fpo = QRect(static_cast<int>(StopM[yg]) + 6, 16, 8, 13);
 
121
                if (fpo.contains(m->pos()))
 
122
                {
 
123
                        ActStop = yg;
 
124
                        emit currStep(DashValues[ActStop]);
 
125
                        repaint();
 
126
                        return;
 
127
                }
 
128
        }
 
129
}
 
130
 
 
131
void DashPreview::mouseReleaseEvent(QMouseEvent *m)
 
132
{
 
133
        m->accept();
 
134
        if ((Mpressed) && (StopM.count() > 2) && (outside || m->y() > 30))
 
135
        {
 
136
                StopM.removeAt(ActStop);
 
137
                DashValues.clear();
 
138
                double startX = 0.0;
 
139
                for (int yg = 0; yg < StopM.count(); ++yg)
 
140
                {
 
141
                        double w = StopM[yg] / 10.0 - startX;
 
142
                        DashValues.append(w);
 
143
                        startX += w;
 
144
                }
 
145
                ActStop = 0;
 
146
                onlyselect = true;
 
147
                Mpressed = false;
 
148
                repaint();
 
149
                emit currStep(DashValues[ActStop]);
 
150
                emit dashChanged();
 
151
                return;
 
152
        }
 
153
        if ((m->y() < height()) && (m->y() > 16) && (m->x() > 0) && (m->x() < width()))
 
154
        {
 
155
                if (ActStop != -1)
 
156
                {
 
157
                        if (m_moveTimer.elapsed() < 250)
 
158
                        {
 
159
                                Mpressed = false;
 
160
                                return;
 
161
                        }
 
162
                        StopM[ActStop] = m->x()-10;
 
163
                }
 
164
                else
 
165
                {
 
166
                        if (DashValues.count() < 10)
 
167
                                StopM.append(m->x()-10);
 
168
                        qSort(StopM.begin(), StopM.end());
 
169
                        ActStop = 0;
 
170
                        for (int yg = 0; yg < StopM.count(); ++yg)
 
171
                        {
 
172
                                QRect fpo = QRect(static_cast<int>(StopM[yg]) + 6, 16, 8, 13);
 
173
                                if (fpo.contains(m->pos()))
 
174
                                {
 
175
                                        ActStop = yg;
 
176
                                        break;
 
177
                                }
 
178
                        }
 
179
                }
 
180
                DashValues.clear();
 
181
                double startX = 0.0;
 
182
                for (int yg = 0; yg < StopM.count(); ++yg)
 
183
                {
 
184
                        double w = StopM[yg] / 10.0 - startX;
 
185
                        DashValues.append(w);
 
186
                        startX += w;
 
187
                }
 
188
                onlyselect = true;
 
189
                repaint();
 
190
                emit currStep(DashValues[ActStop]);
 
191
                emit dashChanged();
 
192
        }
 
193
        Mpressed = false;
 
194
}
 
195
 
 
196
void DashPreview::mouseMoveEvent(QMouseEvent *m)
 
197
{
 
198
        m->accept();
 
199
        QRect fpo;
 
200
        qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 
201
        if ((!Mpressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width()-9) && (DashValues.count() < 10))
 
202
        {
 
203
                qApp->changeOverrideCursor(QCursor(loadIcon("AddPoint.png"), 1, 1));
 
204
                for (int yg = 0; yg < StopM.count(); ++yg)
 
205
                {
 
206
                        fpo = QRect(static_cast<int>(StopM[yg])+6, 16, 8, 13);
 
207
                        if (fpo.contains(m->pos()))
 
208
                        {
 
209
                                qApp->changeOverrideCursor(QCursor(Qt::SizeHorCursor));
 
210
                                return;
 
211
                        }
 
212
                }
 
213
        }
 
214
        if ((Mpressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width()-9) && (ActStop != -1))
 
215
        {
 
216
                qApp->changeOverrideCursor(QCursor(Qt::SizeHorCursor));
 
217
                if (ActStop > 1)
 
218
                {
 
219
                        if (static_cast<int>(StopM[ActStop-1]+10)+2 >= m->x())
 
220
                                return;
 
221
                }
 
222
                if (ActStop < static_cast<int>(StopM.count()-2))
 
223
                {
 
224
                        if (static_cast<int>(StopM[ActStop+1]+10)-2 < m->x())
 
225
                                return;
 
226
                }
 
227
                StopM[ActStop] = m->x()-10;
 
228
                DashValues.clear();
 
229
                double startX = 0.0;
 
230
                for (int yg = 0; yg < StopM.count(); ++yg)
 
231
                {
 
232
                        double w = StopM[yg] / 10.0 - startX;
 
233
                        DashValues.append(w);
 
234
                        startX += w;
 
235
                }
 
236
                onlyselect = true;
 
237
                repaint();
 
238
                startX = 0.0;
 
239
                for (int yg = 0; yg < ActStop; ++yg)
 
240
                {
 
241
                        startX += StopM[yg] / 10.0 - startX;
 
242
                }
 
243
                emit currStep(StopM[ActStop] / 10.0 - startX);
 
244
        }
 
245
        if ((Mpressed) && (outside || m->y() > 30) && (ActStop >= 0) && (StopM.count() > 2))
 
246
                qApp->changeOverrideCursor(QCursor(loadIcon("DelPoint.png"), 1, 1));
 
247
}
 
248
 
 
249
void DashPreview::leaveEvent(QEvent*)
 
250
{
 
251
        if ((Mpressed) && (ActStop >= 0) && (StopM.count() > 2))
 
252
                qApp->changeOverrideCursor(QCursor(loadIcon("DelPoint.png"), 1, 1));
 
253
        else
 
254
                qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 
255
        outside = true;
 
256
}
 
257
 
 
258
void DashPreview::enterEvent(QEvent*)
 
259
{
 
260
        outside = false;
 
261
}
 
262
 
 
263
void DashPreview::setActStep(double t)
 
264
{
 
265
        if (ActStop == -1)
 
266
                return;
 
267
        DashValues[ActStop] = t;
 
268
        onlyselect = true;
 
269
        repaint();
 
270
        emit dashChanged();
 
271
}
 
272
 
 
273
void DashPreview::setDashValues(QVector<double> vals)
 
274
{
 
275
        DashValues = vals;
 
276
        ActStop = 0;
 
277
        onlyselect = true;
 
278
        repaint();
 
279
        if (StopM.count() != 0)
 
280
                emit currStep(StopM[0] / 10.0);
 
281
}
 
282
 
 
283
DashEditor::DashEditor(QWidget *pa) : QFrame(pa)
 
284
{
 
285
        setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
 
286
        setFrameShape( QFrame::Panel );
 
287
        setLineWidth( 1 );
 
288
        QGridLayout *gridLayout = new QGridLayout(this);
 
289
        gridLayout->setSpacing(2);
 
290
        gridLayout->setMargin(2);
 
291
 
 
292
        Preview = new DashPreview(this);
 
293
        gridLayout->addWidget(Preview, 0, 0, 1, 2);
 
294
 
 
295
        Position = new QDoubleSpinBox( this );
 
296
        Position->setDecimals(1);
 
297
        Position->setMinimum(0);
 
298
        Position->setMaximum(100);
 
299
        Position->setSingleStep(0.1);
 
300
        Position->setValue(0);
 
301
        Desc = new QLabel( this );
 
302
 
 
303
        Offset = new QDoubleSpinBox( this );
 
304
        Offset->setDecimals(1);
 
305
        Offset->setMinimum(0);
 
306
        Offset->setMaximum(100);
 
307
        Offset->setSingleStep(0.1);
 
308
        Offset->setValue(0);
 
309
        Desc2 = new QLabel( this );
 
310
        languageChange();
 
311
 
 
312
        gridLayout->addWidget(Desc, 1, 0, 1, 1);
 
313
        gridLayout->addWidget(Position, 1, 1, 1, 1);
 
314
        gridLayout->addWidget(Desc2, 2, 0, 1, 1);
 
315
        gridLayout->addWidget(Offset, 2, 1, 1, 1);
 
316
 
 
317
        connect(Position, SIGNAL(valueChanged(double)), Preview, SLOT(setActStep(double)));
 
318
        connect(Offset, SIGNAL(valueChanged(double)), this, SIGNAL(dashChanged()));
 
319
        connect(Preview, SIGNAL(currStep(double)), this, SLOT(setPos(double)));
 
320
        connect(Preview, SIGNAL(dashChanged()), this, SIGNAL(dashChanged()));
 
321
}
 
322
 
 
323
void DashEditor::setPos(double p)
 
324
{
 
325
        disconnect(Position, SIGNAL(valueChanged(double)), Preview, SLOT(setActStep(double)));
 
326
        Position->setValue(p);
 
327
        connect(Position, SIGNAL(valueChanged(double)), Preview, SLOT(setActStep(double)));
 
328
}
 
329
 
 
330
void DashEditor::setDashValues(QVector<double> vals, double linewidth, double offset)
 
331
{
 
332
        QVector<double> tmp;
 
333
        for (int a = 0; a < vals.count(); a++)
 
334
        {
 
335
                tmp.append(vals[a] / linewidth);
 
336
        }
 
337
        Preview->setDashValues(tmp);
 
338
        disconnect(Offset, SIGNAL(valueChanged(double)), this, SIGNAL(dashChanged()));
 
339
        Offset->setValue(offset / linewidth);
 
340
        connect(Offset, SIGNAL(valueChanged(double)), this, SIGNAL(dashChanged()));
 
341
}
 
342
 
 
343
QVector<double> DashEditor::getDashValues(double linewidth)
 
344
{
 
345
        QVector<double> tmp;
 
346
        for (int a = 0; a < Preview->DashValues.count(); a++)
 
347
        {
 
348
                tmp.append(Preview->DashValues[a] * linewidth);
 
349
        }
 
350
        return tmp;
 
351
}
 
352
 
 
353
void DashEditor::changeEvent(QEvent *e)
 
354
{
 
355
        if (e->type() == QEvent::LanguageChange)
 
356
                languageChange();
 
357
        else
 
358
                QWidget::changeEvent(e);
 
359
}
 
360
 
 
361
void DashEditor::languageChange()
 
362
{
 
363
        Desc->setText( tr( "Value:" ) );
 
364
        Desc2->setText( tr( "Offset:" ) );
 
365
}