Subversion Repositories Scribus

Rev

Rev 18795 | Rev 19067 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16546 jghali 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
#include "propertywidget_distance.h"
9
 
17388 jghali 10
#include "pageitem_table.h"
11
#include "pageitem_textframe.h"
12
#include "scribus.h"
16546 jghali 13
#include "scribusdoc.h"
17388 jghali 14
#include "selection.h"
15
#include "tabmanager.h"
16546 jghali 16
#include "units.h"
17
#include "util_icon.h"
18
 
19
PropertyWidget_Distance::PropertyWidget_Distance(QWidget* parent) : QFrame(parent)
20
{
17388 jghali 21
        m_item = 0;
22
        m_ScMW = 0;
23
 
16546 jghali 24
        setupUi(this);
25
 
26
        setFrameStyle(QFrame::Box | QFrame::Plain);
27
        setLineWidth(1);
28
 
29
        layout()->setAlignment( Qt::AlignTop );
30
 
31
        columnsLabel->setBuddy(columns);
32
        columnGap->setValues(0, 300, 2, 0);
33
 
34
        topDistance->setValues(0, 300, 2, 0);
35
        topLabel->setBuddy(topDistance);
36
 
37
        bottomDistance->setValues(0, 300, 2, 0);
38
        bottomLabel->setBuddy(bottomDistance);
39
 
40
        leftDistance->setValues(0, 300, 2, 0);
41
        leftLabel->setBuddy(leftDistance);
42
 
43
        rightDistance->setValues(0, 300, 2, 0);
44
        rightLabel->setBuddy(rightDistance);
45
 
17420 fschmid 46
        columns->setDecimals(0);
17641 craig 47
        columns->setSuffix("");
17420 fschmid 48
 
16546 jghali 49
        languageChange();
50
 
51
        columnGapLabel->setCurrentIndex(0);
52
}
53
 
17388 jghali 54
void PropertyWidget_Distance::setMainWindow(ScribusMainWindow* mw)
55
{
56
        m_ScMW = mw;
57
 
58
        connect(m_ScMW, SIGNAL(AppModeChanged(int, int)), this, SLOT(handleAppModeChanged(int, int)));
59
        connect(m_ScMW, SIGNAL(UpdateRequest(int))      , this, SLOT(handleUpdateRequest(int)));
60
}
61
 
16546 jghali 62
void PropertyWidget_Distance::setDoc(ScribusDoc *d)
63
{
64
        if(d == (ScribusDoc*) m_doc)
65
                return;
66
 
17388 jghali 67
        if (m_doc)
68
        {
69
                disconnect(m_doc->m_Selection, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()));
70
                disconnect(m_doc             , SIGNAL(docChanged())      , this, SLOT(handleSelectionChanged()));
71
        }
72
 
16546 jghali 73
        m_doc  = d;
17388 jghali 74
        m_item = NULL;
16546 jghali 75
 
76
        if (m_doc.isNull())
17388 jghali 77
        {
78
                disconnectSignals();
16546 jghali 79
                return;
17388 jghali 80
        }
16546 jghali 81
 
82
        m_unitRatio   = m_doc->unitRatio();
83
        m_unitIndex   = m_doc->unitIndex();
84
 
17420 fschmid 85
        columns->setDecimals(0);
16546 jghali 86
        columnGap->setDecimals(2);
87
        topDistance->setDecimals(2);
88
        leftDistance->setDecimals(2);
89
        bottomDistance->setDecimals(2);
90
        rightDistance->setDecimals(2);
17388 jghali 91
 
92
        connect(m_doc->m_Selection, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()));
93
        connect(m_doc             , SIGNAL(docChanged())      , this, SLOT(handleSelectionChanged()));
16546 jghali 94
}
95
 
17388 jghali 96
void PropertyWidget_Distance::setCurrentItem(PageItem *item)
16546 jghali 97
{
17388 jghali 98
        if (!m_ScMW || m_ScMW->scriptIsRunning())
99
                return;
100
        //CB We shouldnt really need to process this if our item is the same one
101
        //maybe we do if the item has been changed by scripter.. but that should probably
102
        //set some status if so.
103
        //FIXME: This wont work until when a canvas deselect happens, m_item must be NULL.
104
        //if (m_item == i)
105
        //      return;
106
 
107
        if (item && m_doc.isNull())
108
                setDoc(item->doc());
109
 
110
        m_item = item;
111
 
112
        disconnectSignals();
113
        configureWidgets();
114
 
115
        if (!m_item) return;
116
 
117
        PageItem_TextFrame *textItem = m_item->asTextFrame();
118
        if (m_doc->appMode == modeEditTable)
119
                textItem = m_item->asTable()->activeCell().textFrame();
120
        if (!textItem) return;
121
 
122
        columns->setMaximum(qMax(qRound(textItem->width() / qMax(textItem->ColGap, 10.0)), 1));
123
        columns->setMinimum(1);
124
        columns->setValue(textItem->Cols);
125
        columnGap->setMinimum(0);
126
        if (columnGapLabel->currentIndex() == 0)
16546 jghali 127
        {
17388 jghali 128
                columnGap->setMaximum(qMax((textItem->width() / textItem->Cols - textItem->textToFrameDistLeft() - textItem->textToFrameDistRight()) * m_unitRatio, 0.0));
129
                columnGap->setValue(textItem->ColGap*m_unitRatio);
130
        }
131
        else
132
        {
133
                columnGap->setMaximum(qMax((textItem->width() / textItem->Cols) * m_unitRatio, 0.0));
134
                columnGap->setValue(textItem->columnWidth() * m_unitRatio);
135
        }
136
        leftDistance->setValue(textItem->textToFrameDistLeft()*m_unitRatio);
137
        topDistance->setValue(textItem->textToFrameDistTop()*m_unitRatio);
138
        bottomDistance->setValue(textItem->textToFrameDistBottom()*m_unitRatio);
139
        rightDistance->setValue(textItem->textToFrameDistRight()*m_unitRatio);
140
        if (columns->value() == 1)
141
        {
142
                columnGap->setEnabled(false);
143
                columnGapLabel->setEnabled(false);
144
        }
145
        else
146
        {
147
                columnGap->setEnabled(true);
148
                columnGapLabel->setEnabled(true);
149
        }
150
 
18795 jghali 151
        showTextDistances(textItem->textToFrameDistLeft(), textItem->textToFrameDistTop(), textItem->textToFrameDistBottom(), textItem->textToFrameDistRight());
18889 fschmid 152
        verticalAlign->setCurrentIndex(textItem->verticalAlignment());
17388 jghali 153
        connectSignals();
154
}
155
 
156
void PropertyWidget_Distance::connectSignals()
157
{
17420 fschmid 158
        connect(columns       , SIGNAL(valueChanged(double))   , this, SLOT(handleColumns()), Qt::UniqueConnection);
17388 jghali 159
        connect(columnGap     , SIGNAL(valueChanged(double)), this, SLOT(handleColumnGap()), Qt::UniqueConnection);
160
        connect(columnGapLabel, SIGNAL(activated(int))      , this, SLOT(handleGapSwitch()), Qt::UniqueConnection);
161
        connect(topDistance   , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()), Qt::UniqueConnection);
162
        connect(leftDistance  , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()), Qt::UniqueConnection);
163
        connect(rightDistance , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()), Qt::UniqueConnection);
164
        connect(bottomDistance, SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()), Qt::UniqueConnection);
165
        connect(tabsButton    , SIGNAL(clicked())           , this, SLOT(handleTabs()), Qt::UniqueConnection);
18889 fschmid 166
        connect(verticalAlign , SIGNAL(activated(int))      , this, SLOT(handleVAlign()), Qt::UniqueConnection);
17388 jghali 167
}
168
 
169
void PropertyWidget_Distance::disconnectSignals()
170
{
17420 fschmid 171
        disconnect(columns       , SIGNAL(valueChanged(double))   , this, SLOT(handleColumns()));
17388 jghali 172
        disconnect(columnGap     , SIGNAL(valueChanged(double)), this, SLOT(handleColumnGap()));
173
        disconnect(columnGapLabel, SIGNAL(activated(int))      , this, SLOT(handleGapSwitch()));
174
        disconnect(topDistance   , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()));
175
        disconnect(leftDistance  , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()));
176
        disconnect(rightDistance , SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()));
177
        disconnect(bottomDistance, SIGNAL(valueChanged(double)), this, SLOT(handleTextDistances()));
178
        disconnect(tabsButton    , SIGNAL(clicked())           , this, SLOT(handleTabs()));
18889 fschmid 179
        disconnect(verticalAlign , SIGNAL(activated(int))      , this, SLOT(handleVAlign()));
17388 jghali 180
}
181
 
182
void PropertyWidget_Distance::configureWidgets(void)
183
{
184
        bool enabled = false;
185
        if (m_item && m_doc)
186
        {
187
                PageItem_TextFrame *textItem = m_item->asTextFrame();
188
                if (m_doc->appMode == modeEditTable)
189
                        textItem = m_item->asTable()->activeCell().textFrame();
190
 
191
                enabled  = (textItem != NULL);
192
                enabled &= (m_doc->m_Selection->count() == 1);
193
 
194
                if (textItem)
195
                {
196
                        int numCols = textItem->Cols;
197
 
198
                        columnGap->setEnabled(numCols != 1);
199
                        columnGapLabel->setEnabled(numCols != 1);
200
                }
201
        }
202
        setEnabled(enabled);
203
}
204
 
205
void PropertyWidget_Distance::handleAppModeChanged(int oldMode, int mode)
206
{
207
        if (oldMode == modeEditTable || mode == modeEditTable)
208
        {
209
                setCurrentItem(m_item);
210
        }
211
}
212
 
213
void PropertyWidget_Distance::handleSelectionChanged()
214
{
215
        if (!m_doc || !m_ScMW || m_ScMW->scriptIsRunning())
16546 jghali 216
                return;
17388 jghali 217
 
218
        PageItem* currItem = currentItemFromSelection();
219
        setCurrentItem(currItem);
220
        updateGeometry();
221
        repaint();
222
}
223
 
224
void PropertyWidget_Distance::handleUpdateRequest(int /*updateFlags*/)
225
{
226
        // Nothing to do in this widget
227
}
228
 
18795 jghali 229
void PropertyWidget_Distance::showColumns(int r, double g)
17388 jghali 230
{
231
        if (!m_ScMW || m_ScMW->scriptIsRunning())
232
                return;
233
 
234
        bool cSigWasBlocked    = columns->blockSignals(true);
235
        bool cGapSigWasBlocked = columnGap->blockSignals(true);
236
 
237
        columns->setValue(r);
238
        columnGap->setValue(g * m_unitRatio);
239
        if (m_item)
240
        {
241
                PageItem_TextFrame *textItem = m_item->asTextFrame();
242
                if (m_doc->appMode == modeEditTable)
243
                        textItem = m_item->asTable()->activeCell().textFrame();
244
 
245
                if (textItem != 0)
246
                {
247
                        columns->setMaximum(qMax(qRound(textItem->width() / qMax(textItem->ColGap, 10.0)), 1));
248
                        if (columnGapLabel->currentIndex() == 0)
249
                        {
250
                                columnGap->setMaximum(qMax((textItem->width() / textItem->Cols - textItem->textToFrameDistLeft() - textItem->textToFrameDistRight()) * m_unitRatio, 0.0));
251
                                columnGap->setValue(textItem->ColGap * m_unitRatio);
252
                        }
253
                        else
254
                        {
255
                                columnGap->setMaximum(qMax((textItem->width() / textItem->Cols) * m_unitRatio, 0.0));
256
                                columnGap->setValue(textItem->columnWidth() * m_unitRatio);
257
                        }
258
                }
16546 jghali 259
        }
17388 jghali 260
        columns->setMinimum(1);
261
        columnGap->setMinimum(0);
262
        columnGap->setEnabled(columns->value() != 1);
263
        columnGapLabel->setEnabled(columns->value() != 1);
264
 
265
        columns->blockSignals(cSigWasBlocked);
266
        columnGap->blockSignals(cGapSigWasBlocked);
16546 jghali 267
}
268
 
18795 jghali 269
void PropertyWidget_Distance::showTextDistances(double left, double top, double bottom, double right)
16546 jghali 270
{
17388 jghali 271
        leftDistance->showValue(left * m_unitRatio);
272
        topDistance->showValue(top * m_unitRatio);
273
        bottomDistance->showValue(bottom * m_unitRatio);
274
        rightDistance->showValue(right * m_unitRatio);
16546 jghali 275
}
276
 
17388 jghali 277
void PropertyWidget_Distance::handleColumns()
16546 jghali 278
{
17388 jghali 279
        if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
16546 jghali 280
                return;
281
 
17388 jghali 282
        PageItem *textItem = m_item;
283
        if (m_doc->appMode == modeEditTable)
284
                textItem = m_item->asTable()->activeCell().textFrame();
16546 jghali 285
 
17388 jghali 286
        if (textItem)
287
        {
17641 craig 288
                textItem->setColumns(static_cast<int>(columns->value()));
18795 jghali 289
                showColumns(textItem->Cols, textItem->ColGap);
290
                //this is already done in showColumns()
17641 craig 291
                /*if (static_cast<int>(columns->value()) == 1)
17388 jghali 292
                {
293
                        columnGap->setEnabled(false);
294
                        columnGapLabel->setEnabled(false);
295
                }
296
                else
297
                {
298
                        columnGap->setEnabled(true);
299
                        columnGapLabel->setEnabled(true);
17641 craig 300
                }*/
17388 jghali 301
                textItem->update();
302
                if (m_doc->appMode == modeEditTable)
303
                        m_item->asTable()->update();
304
                m_doc->regionsChanged()->update(QRect());
305
        }
306
}
16546 jghali 307
 
17388 jghali 308
void PropertyWidget_Distance::handleColumnGap()
309
{
310
        if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
311
                return;
16546 jghali 312
 
17388 jghali 313
        PageItem *textItem = m_item;
314
        if (m_doc->appMode == modeEditTable)
315
                textItem = m_item->asTable()->activeCell().textFrame();
316
        if (!textItem) return;
317
 
318
        if (columnGapLabel->currentIndex() == 0)
17641 craig 319
                textItem->setColumnGap(columnGap->value() / m_unitRatio);
17388 jghali 320
        else
321
        {
322
                double lineCorr;
323
                if ((textItem->lineColor() != CommonStrings::None) || (!textItem->strokePattern().isEmpty()))
324
                        lineCorr = textItem->lineWidth();
325
                else
326
                        lineCorr = 0;
327
                double newWidth = columnGap->value() / m_unitRatio;
328
                double newGap = qMax(((textItem->width() - textItem->textToFrameDistLeft() - textItem->textToFrameDistRight() - lineCorr) - (newWidth * textItem->Cols)) / (textItem->Cols - 1), 0.0);
17641 craig 329
                textItem->setColumnGap(newGap);
17388 jghali 330
        }
331
        textItem->update();
332
        if (m_doc->appMode == modeEditTable)
333
                m_item->asTable()->update();
334
        m_doc->regionsChanged()->update(QRect());
16546 jghali 335
}
336
 
17388 jghali 337
void PropertyWidget_Distance::handleGapSwitch()
338
{
339
        if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
340
                return;
341
        PageItem *textItem = m_item;
342
        if (m_doc->appMode == modeEditTable)
343
                textItem = m_item->asTable()->activeCell().textFrame();
344
        if (textItem != NULL)
18795 jghali 345
                showColumns(textItem->Cols, textItem->ColGap);
17388 jghali 346
 
347
        int index = columnGapLabel->currentIndex();
348
        columnGap->setToolTip((index == 0) ? tr( "Distance between columns" ) : tr( "Column width" ));
349
}
350
 
18889 fschmid 351
void PropertyWidget_Distance::handleVAlign()
352
{
353
        if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
354
                return;
355
        PageItem *textItem = m_item;
356
        if (m_doc->appMode == modeEditTable)
357
                textItem = m_item->asTable()->activeCell().textFrame();
358
        if (textItem != NULL)
359
        {
360
                textItem->setVerticalAlignment(verticalAlign->currentIndex());
361
                textItem->update();
362
                if (m_doc->appMode == modeEditTable)
363
                        m_item->asTable()->update();
364
                m_doc->regionsChanged()->update(QRect());
365
        }
366
}
367
 
17388 jghali 368
void PropertyWidget_Distance::handleTabs()
369
{
370
        if (m_doc && m_item)
371
        {
372
                PageItem_TextFrame *tItem = m_item->asTextFrame();
373
                if (tItem == 0)
374
                        return;
375
                const ParagraphStyle& style(m_doc->appMode == modeEdit ? tItem->currentStyle() : tItem->itemText.defaultStyle());
376
                TabManager *dia = new TabManager(this, m_doc->unitIndex(), style.tabValues(), tItem->columnWidth());
377
                if (dia->exec())
378
                {
379
                        if (m_doc->appMode != modeEdit)
380
                        {
381
                                ParagraphStyle newStyle(m_item->itemText.defaultStyle());
382
                                newStyle.setTabValues(dia->tmpTab);
17641 craig 383
                                Selection tempSelection(this, false);
384
                                tempSelection.addItem(m_item, true);
385
                                m_doc->itemSelection_ApplyParagraphStyle(newStyle, &tempSelection);
17388 jghali 386
                        }
387
                        else
388
                        {
389
                                ParagraphStyle newStyle;
390
                                newStyle.setTabValues(dia->tmpTab);
391
                                m_doc->itemSelection_ApplyParagraphStyle(newStyle);
392
                        }
393
                        m_item->update();
394
                }
395
                delete dia;
396
        }
397
}
398
 
399
void PropertyWidget_Distance::handleTextDistances()
400
{
401
        if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
402
                return;
403
 
404
        PageItem *textItem = m_item;
405
        if (m_doc->appMode == modeEditTable)
406
                textItem = m_item->asTable()->activeCell().textFrame();
407
        if (!textItem) return;
408
 
409
        double left   = leftDistance->value() / m_unitRatio;
410
        double right  = rightDistance->value() / m_unitRatio;
411
        double top    = topDistance->value() / m_unitRatio;
412
        double bottom = bottomDistance->value() / m_unitRatio;
413
        textItem->setTextToFrameDist(left, right, top, bottom);
18795 jghali 414
        showColumns(textItem->Cols, textItem->ColGap);
17388 jghali 415
 
416
        textItem->update();
417
        if (m_doc->appMode == modeEditTable)
418
                m_item->asTable()->update();
419
        m_doc->regionsChanged()->update(QRect());
420
}
421
 
422
void PropertyWidget_Distance::changeEvent(QEvent *e)
423
{
424
        if (e->type() == QEvent::LanguageChange)
425
        {
426
                languageChange();
427
                return;
428
        }
429
        QWidget::changeEvent(e);
430
}
431
 
16546 jghali 432
void PropertyWidget_Distance::languageChange()
433
{
434
        columnsLabel->setText( tr("Colu&mns:"));
435
 
18889 fschmid 436
        int oldAliLabel = verticalAlign->currentIndex();
437
        verticalAlign->clear();
438
        verticalAlign->addItem( tr("Top"));
439
        verticalAlign->addItem( tr("Middle"));
440
        verticalAlign->addItem( tr("Bottom"));
441
        verticalAlign->setCurrentIndex(oldAliLabel);
442
 
16546 jghali 443
        int oldcolgapLabel = columnGapLabel->currentIndex();
444
        columnGapLabel->clear();
445
        columnGapLabel->addItem( tr("Gap:"));
446
        columnGapLabel->addItem( tr("Width:"));
447
        columnGapLabel->setCurrentIndex(oldcolgapLabel);
448
 
449
        topLabel->setText( tr("To&p:"));
450
        bottomLabel->setText( tr("&Bottom:"));
451
        leftLabel->setText( tr("&Left:"));
452
        rightLabel->setText( tr("&Right:"));
453
 
454
        tabsButton->setText( tr("T&abulators..."));
455
 
456
        QString ptSuffix = tr(" pt");
457
 
458
        QString suffix = (m_doc) ? unitGetSuffixFromIndex(m_doc->unitIndex()) : ptSuffix;
459
 
460
        columnGap->setSuffix(suffix);
461
        leftDistance->setSuffix(suffix);
462
        topDistance->setSuffix(suffix);
463
        bottomDistance->setSuffix(suffix);
464
        rightDistance->setSuffix(suffix);
465
 
466
        columns->setToolTip( tr("Number of columns in text frame"));
467
        columnGapLabel->setToolTip( tr("Switches between Gap or Column width"));
468
        columnGap->setToolTip( tr("Distance between columns"));
469
        topDistance->setToolTip( tr("Distance of text from top of frame"));
470
        bottomDistance->setToolTip( tr("Distance of text from bottom of frame"));
471
        leftDistance->setToolTip( tr("Distance of text from left of frame"));
472
        rightDistance->setToolTip( tr("Distance of text from right of frame"));
473
        tabsButton->setToolTip( tr("Edit tab settings of text frame..."));
17388 jghali 474
}
475
 
476
void PropertyWidget_Distance::unitChange()
477
{
478
        if (!m_doc)
479
                return;
480
 
481
        m_unitRatio = m_doc->unitRatio();
482
        m_unitIndex = m_doc->unitIndex();
483
 
484
        columnGap->blockSignals(true);
485
        leftDistance->blockSignals(true);
486
        topDistance->blockSignals(true);
487
        bottomDistance->blockSignals(true);
488
        rightDistance->blockSignals(true);
489
 
490
        columnGap->setNewUnit( m_unitIndex );
491
        leftDistance->setNewUnit( m_unitIndex );
492
        topDistance->setNewUnit( m_unitIndex );
493
        bottomDistance->setNewUnit( m_unitIndex );
494
        rightDistance->setNewUnit( m_unitIndex );
495
 
496
        columnGap->blockSignals(false);
497
        leftDistance->blockSignals(false);
498
        topDistance->blockSignals(false);
499
        bottomDistance->blockSignals(false);
500
        rightDistance->blockSignals(false);
17641 craig 501
}