Subversion Repositories Scribus

Rev

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