Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
4430 cbradney 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
*/
1111 tsoots 7
/***************************************************************************
8
 *   Copyright (C) 2005 by Riku Leino                                      *
9
 *   tsoots@gmail.com                                                      *
10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 *   This program is distributed in the hope that it will be useful,       *
17
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19
 *   GNU General Public License for more details.                          *
20
 *                                                                         *
21
 *   You should have received a copy of the GNU General Public License     *
22
 *   along with this program; if not, write to the                         *
23
 *   Free Software Foundation, Inc.,                                       *
24
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25
 ***************************************************************************/
26
 
27
#include <qlayout.h>
28
#include <qpixmap.h>
29
#include <qpushbutton.h>
1142 tsoots 30
#include <qrect.h>
31
#include <qstyle.h>
1111 tsoots 32
#include <qtooltip.h>
33
#include <qcheckbox.h>
1167 tsoots 34
#include <qfont.h>
35
#include <qfontmetrics.h>
1288 cbradney 36
 
37
#include "undogui.h"
38
#include "undogui.moc"
2856 cbradney 39
#include "prefsmanager.h"
1288 cbradney 40
#include "prefsfile.h"
41
#include "prefscontext.h"
5781 cbradney 42
#include "scribuscore.h"
1288 cbradney 43
#include "menumanager.h"
3251 craig 44
#include "scraction.h"
1111 tsoots 45
 
46
extern QPixmap loadIcon(QString nam);
47
 
3205 craig 48
 
3923 tsoots 49
UndoGui::UndoGui(QWidget* parent, const char* name, WFlags f) : ScrPaletteBase(parent, name, f)
1111 tsoots 50
{
51
 
52
}
53
 
54
/*** UndoWidget ***************************************************************/
55
 
56
UndoWidget::UndoWidget(QWidget* parent, const char* name)
57
: UndoGui(parent, name)
58
{
1288 cbradney 59
        /* BnF standard toolbar buttons
1111 tsoots 60
        QHBoxLayout* layout = new QHBoxLayout(this, 0, 0, "layout");
61
 
1288 cbradney 62
 
1111 tsoots 63
        undoButton = new QToolButton(this, "undoButton");
64
        undoButton->setIconSet(loadIcon("u_undo.png"));
65
        undoButton->setUsesTextLabel(false);
66
        QToolTip::add(undoButton, tr("Undo"));
67
        layout->addWidget(undoButton);
68
        undoMenu = new QPopupMenu(undoButton, "undoMenu");
69
        undoButton->setPopup(undoMenu);
70
        undoButton->setPopupDelay(0);
1138 tsoots 71
        undoButton->setAutoRaise(true);
1111 tsoots 72
 
73
        redoButton = new QToolButton(this, "redoButton");
74
        redoButton->setIconSet(loadIcon("u_redo.png"));
75
        redoButton->setUsesTextLabel(false);
76
        QToolTip::add(redoButton, tr("Redo"));
77
        layout->addWidget(redoButton);
78
        redoMenu = new QPopupMenu(redoButton, "redoMenu");
79
        redoButton->setPopup(redoMenu);
80
        redoButton->setPopupDelay(0);
1138 tsoots 81
        redoButton->setAutoRaise(true);
1288 cbradney 82
        */
83
        //Scribus action based toolbar button construction
5781 cbradney 84
        ScCore->primaryMainWindow()->scrActions["editUndoAction"]->addTo(parent);
85
        ScCore->primaryMainWindow()->scrActions["editRedoAction"]->addTo(parent);
86
        ScCore->primaryMainWindow()->scrMenuMgr->createMenu("undoButtonMenu", "undoButtonMenu");
87
        ScCore->primaryMainWindow()->scrMenuMgr->createMenu("redoButtonMenu", "redoButtonMenu");
88
        undoMenu=ScCore->primaryMainWindow()->scrMenuMgr->getLocalPopupMenu("undoButtonMenu");
89
        redoMenu=ScCore->primaryMainWindow()->scrMenuMgr->getLocalPopupMenu("redoButtonMenu");
90
        ScCore->primaryMainWindow()->scrMenuMgr->addMenuToWidgetOfAction("undoButtonMenu", ScCore->primaryMainWindow()->scrActions["editUndoAction"]);
91
        ScCore->primaryMainWindow()->scrMenuMgr->addMenuToWidgetOfAction("redoButtonMenu", ScCore->primaryMainWindow()->scrActions["editRedoAction"]);
92
        QToolButton *undoButton = dynamic_cast<QToolButton*>(ScCore->primaryMainWindow()->scrActions["editUndoAction"]->getWidgetAddedTo());
93
        QToolButton *redoButton = dynamic_cast<QToolButton*>(ScCore->primaryMainWindow()->scrActions["editRedoAction"]->getWidgetAddedTo());
1337 cbradney 94
        if (undoButton && redoButton)
95
        {
96
                undoButton->setPopupDelay(0);
97
                redoButton->setPopupDelay(0);
98
        }
1288 cbradney 99
 
5781 cbradney 100
        ScCore->primaryMainWindow()->scrActions["editCut"]->addTo(parent);
101
        ScCore->primaryMainWindow()->scrActions["editCopy"]->addTo(parent);
102
        ScCore->primaryMainWindow()->scrActions["editPaste"]->addTo(parent);
5800 fschmid 103
        ScCore->primaryMainWindow()->scrMenuMgr->addMenuToWidgetOfAction("EditPasteRecent", ScCore->primaryMainWindow()->scrActions["editPaste"]);
1288 cbradney 104
 
105
        /* BnF Undo buttons
1111 tsoots 106
        connect(undoButton, SIGNAL(clicked()), this, SLOT(undoClicked()));
107
        connect(redoButton, SIGNAL(clicked()), this, SLOT(redoClicked()));
1288 cbradney 108
        */
1111 tsoots 109
        connect(undoMenu, SIGNAL(activated(int)), this, SLOT(undoMenuClicked(int)));
110
        connect(redoMenu, SIGNAL(activated(int)), this, SLOT(redoMenuClicked(int)));
111
}
112
 
113
void UndoWidget::clear()
114
{
115
        undoMenu->clear();
116
        undoItems.clear();
1288 cbradney 117
        //Scribus disable
5781 cbradney 118
        ScCore->primaryMainWindow()->scrActions["editUndoAction"]->setEnabled(false);
1288 cbradney 119
        // BnF disable
120
        //undoButton->setEnabled(false);
1111 tsoots 121
        redoMenu->clear();
122
        redoItems.clear();
1288 cbradney 123
        //Scribus disable;
5781 cbradney 124
        ScCore->primaryMainWindow()->scrActions["editRedoAction"]->setEnabled(false);
1288 cbradney 125
        // BnF disable
126
        //redoButton->setEnabled(false);
1111 tsoots 127
}
128
 
129
void UndoWidget::undoClicked()
130
{
131
        if (undoItems.size() > 0)
132
                emit undo(1);
133
}
134
 
135
void UndoWidget::redoClicked()
136
{
137
        if (redoItems.size() > 0)
138
                emit redo(1);
139
}
140
 
141
void UndoWidget::undoMenuClicked(int id)
142
{
143
        int steps = undoMenu->indexOf(id) + 1;
144
        emit undo(steps);
145
}
146
 
147
void UndoWidget::redoMenuClicked(int id)
148
{
149
        int steps = redoMenu->indexOf(id) + 1;
150
        emit redo(steps);
151
}
152
 
1336 tsoots 153
void UndoWidget::insertUndoItem(UndoObject* target, UndoState* state)
1111 tsoots 154
{
2397 cbradney 155
        undoItems.insert(undoItems.begin(), QString( tr("%1: %2", "undo target: action (f.e. Text frame: Resize)"))
1336 tsoots 156
                                        .arg(target->getUName()).arg(state->getName()));
1457 tsoots 157
        clearRedo();
1111 tsoots 158
        updateUndoMenu();
1443 tsoots 159
        updateRedoMenu();
1111 tsoots 160
}
161
 
1336 tsoots 162
void UndoWidget::insertRedoItem(UndoObject* target, UndoState* state)
1111 tsoots 163
{
2397 cbradney 164
        redoItems.push_back(QString( tr("%1: %2", "undo target: action (f.e. Text frame: Resize)"))
1336 tsoots 165
                        .arg(target->getUName()).arg(state->getName()));
1111 tsoots 166
        updateRedoMenu();
1443 tsoots 167
        updateUndoMenu();
1111 tsoots 168
}
169
 
1457 tsoots 170
void UndoWidget::clearRedo()
1111 tsoots 171
{
172
        redoItems.erase(redoItems.begin(), redoItems.end());
173
        updateRedoMenu();
174
}
175
 
176
void UndoWidget::updateUndoMenu()
177
{
178
        undoMenu->clear();
179
        for (uint i = 0; i < MENU_HEIGHT && i < undoItems.size(); ++i)
180
                undoMenu->insertItem(undoItems[i]);
1288 cbradney 181
        //BnF
182
        //undoButton->setEnabled(undoMenu->count() != 0);
183
        //SCribus
4985 cbradney 184
        //ScMW->scrActions["editUndoAction"]->setEnabled(undoMenu->count() != 0);
185
        updateUndoActions();
1111 tsoots 186
}
187
 
188
void UndoWidget::updateRedoMenu()
189
{
190
        redoMenu->clear();
191
        for (uint i = 0; i < MENU_HEIGHT && i < redoItems.size(); ++i)
192
                redoMenu->insertItem(redoItems[i]);
1288 cbradney 193
        //BnF
194
        //redoButton->setEnabled(redoMenu->count() != 0);
195
        //Scribus
4985 cbradney 196
        //ScMW->scrActions["editRedoAction"]->setEnabled(redoMenu->count() != 0);
197
        updateUndoActions();
198
}
199
 
200
void UndoWidget::updateUndoActions()
201
{
5781 cbradney 202
        ScCore->primaryMainWindow()->scrActions["editUndoAction"]->setEnabled(undoMenu->count() != 0);
203
        ScCore->primaryMainWindow()->scrActions["editRedoAction"]->setEnabled(redoMenu->count() != 0);
1111 tsoots 204
}
205
 
206
void UndoWidget::updateUndo(int steps)
207
{
208
        for (int i = 0; i < steps; ++i)
209
        {
210
                redoItems.insert(redoItems.begin(), undoItems[0]);
211
                undoItems.erase(undoItems.begin());    
212
        }
213
        updateUndoMenu();
214
        updateRedoMenu();
215
}
216
 
217
void UndoWidget::updateRedo(int steps)
218
{
219
        for (int i = 0; i < steps; ++i)
220
        {
221
                undoItems.insert(undoItems.begin(), redoItems[0]);
222
                redoItems.erase(redoItems.begin());    
223
        }
224
        updateUndoMenu();
225
        updateRedoMenu();
226
}
227
 
228
void UndoWidget::popBack()
229
{
1197 tsoots 230
        if (undoItems.size() > 0)
231
        {
232
                undoItems.erase(undoItems.end() - 1);
233
                updateUndoMenu();
234
        }
1111 tsoots 235
}
236
 
237
UndoWidget::~UndoWidget()
238
{
239
 
240
}
241
 
242
/*** UndoPalette **************************************************************/
243
 
244
UndoPalette::UndoPalette(QWidget* parent, const char* name)
3923 tsoots 245
: UndoGui(parent, name)
1111 tsoots 246
{
247
        currentSelection = 0;
248
        redoItems = 0;
249
        QVBoxLayout* layout = new QVBoxLayout(this, 5, 5, "layout");
250
 
2198 cbradney 251
        objectBox = new QCheckBox(this, "objectBox");
1111 tsoots 252
        layout->addWidget(objectBox);
1443 tsoots 253
//      objectBox->setEnabled(false);
1111 tsoots 254
 
255
        undoList = new QListBox(this, "undoList");
256
        undoList->setMultiSelection(false);
257
        undoList->setSelectionMode(QListBox::Single);
258
        layout->addWidget(undoList);
1393 cbradney 259
 
2198 cbradney 260
        QHBoxLayout* buttonLayout = new QHBoxLayout(0, 0, 5, "buttonLayout");
5179 cbradney 261
        undoButton = new QPushButton(loadIcon("16/edit-undo.png"), "", this, "undoButton");
1111 tsoots 262
        buttonLayout->addWidget(undoButton);
5179 cbradney 263
        redoButton = new QPushButton(loadIcon("16/edit-redo.png"), "", this, "redoButton");
2198 cbradney 264
        buttonLayout->addWidget(redoButton);
1441 cbradney 265
        //Save the translated key sequence - hopefully we get the translated one here!
266
        initialUndoKS = undoButton->accel();
267
        initialRedoKS = redoButton->accel();
1111 tsoots 268
        layout->addLayout(buttonLayout);
269
 
1441 cbradney 270
        updateFromPrefs();
2198 cbradney 271
        languageChange();
5257 cbradney 272
        connect(PrefsManager::instance(), SIGNAL(prefsChanged()), this, SLOT(updateFromPrefs()));
1111 tsoots 273
        connect(undoButton, SIGNAL(clicked()), this, SLOT(undoClicked()));
274
        connect(redoButton, SIGNAL(clicked()), this, SLOT(redoClicked()));
275
        connect(undoList, SIGNAL(highlighted(int)), this, SLOT(undoListClicked(int)));
1200 tsoots 276
        connect(undoList, SIGNAL(onItem(QListBoxItem*)), this, SLOT(showToolTip(QListBoxItem*)));
1201 tsoots 277
        connect(undoList, SIGNAL(onViewport()), this, SLOT(removeToolTip()));
1443 tsoots 278
        connect(objectBox, SIGNAL(toggled(bool)), this, SLOT(objectCheckBoxClicked(bool)));
5781 cbradney 279
        connect(ScCore->primaryMainWindow()->scrActions["editActionMode"], SIGNAL(toggled(bool)),
1446 tsoots 280
                objectBox, SLOT(setChecked(bool)));
281
        connect(objectBox, SIGNAL(toggled(bool)),
5781 cbradney 282
                        ScCore->primaryMainWindow()->scrActions["editActionMode"], SLOT(setOn(bool)));
1111 tsoots 283
}
284
 
285
void UndoPalette::clear()
286
{
287
        undoList->clear();
2397 cbradney 288
        undoList->insertItem( tr("Initial State"));
1111 tsoots 289
        undoButton->setEnabled(false);
290
        redoButton->setEnabled(false);
291
}
292
 
1441 cbradney 293
void UndoPalette::updateFromPrefs()
294
{
5781 cbradney 295
        undoButton->setAccel(ScCore->primaryMainWindow()->scrActions["editUndoAction"]->accel());
296
        redoButton->setAccel(ScCore->primaryMainWindow()->scrActions["editRedoAction"]->accel());
1441 cbradney 297
}
298
 
2198 cbradney 299
void UndoPalette::languageChange()
300
{
2397 cbradney 301
        setCaption( tr("Action History"));     
302
        objectBox->setText( tr("Show selected object only"));
303
        undoButton->setText( tr("&Undo"));
304
        redoButton->setText( tr("&Redo"));
2198 cbradney 305
}
306
 
1111 tsoots 307
void UndoPalette::insertUndoItem(UndoObject* target, UndoState* state)
308
{
1457 tsoots 309
        clearRedo();
1200 tsoots 310
        undoList->insertItem(new UndoItem(target->getUName(), state->getName(),
1250 tsoots 311
                         state->getDescription(), target->getUPixmap(),
5950 tsoots 312
                         state->getPixmap(), true));
1111 tsoots 313
        currentSelection = undoList->numRows() - 1;
314
        updateList();
315
}
316
 
317
void UndoPalette::insertRedoItem(UndoObject* target, UndoState* state)
318
{
1190 tsoots 319
        if (undoList->count() == 1)
1443 tsoots 320
        {
1190 tsoots 321
                undoList->setSelected(0, true);
1443 tsoots 322
                currentSelection = 0;
323
        }
1200 tsoots 324
        undoList->insertItem(new UndoItem(target->getUName(), state->getName(),
1250 tsoots 325
                         state->getDescription(), target->getUPixmap(),
5950 tsoots 326
                         state->getPixmap(), false));
1111 tsoots 327
        updateList();
328
}
329
 
330
void UndoPalette::updateUndo(int steps)
331
{
332
        if (undoList->currentItem() == currentSelection)
333
        {
334
                currentSelection -= steps;
335
                updateList();
336
        }
337
}
338
 
339
void UndoPalette::updateRedo(int steps)
340
{
341
        if (undoList->currentItem() == currentSelection)
342
        {
343
                currentSelection += steps;
344
                updateList();
345
        }
346
}
347
 
348
void UndoPalette::popBack()
349
{
1197 tsoots 350
        if (undoList->count() > 1)
351
        {
352
                undoList->removeItem(0);
353
                currentSelection = undoList->numRows() - 1;
354
        }
1111 tsoots 355
}
356
 
357
void UndoPalette::updateList()
358
{
1167 tsoots 359
        undoList->setCurrentItem(currentSelection);
1111 tsoots 360
        undoList->setSelected(currentSelection, true);
361
        redoButton->setEnabled(currentSelection < undoList->numRows() - 1);
362
        undoButton->setEnabled(currentSelection > 0);
363
        if (!undoList->itemVisible(currentSelection))
364
                undoList->setBottomItem(currentSelection);
5950 tsoots 365
        for (int i = 0; i < undoList->numRows(); ++i)
366
        {
367
                UndoItem *item = dynamic_cast<UndoItem*>(undoList->item(i));
368
                if (!item)
369
                        continue;
370
 
371
                item->setUndoAction(currentSelection >= i);
372
        }
1111 tsoots 373
}
374
 
4985 cbradney 375
void UndoPalette::updateUndoActions()
376
{
377
        //ScMW->scrActions["editUndoAction"]->setEnabled(currentSelection > 0);
378
        //ScMW->scrActions["editRedoAction"]->setEnabled(currentSelection < undoList->numRows() - 1);
379
}
380
 
1457 tsoots 381
void UndoPalette::clearRedo()
1111 tsoots 382
{
383
        for (int i = (undoList->numRows() - 1); i > currentSelection; --i)
384
                undoList->removeItem(i);
385
}
386
 
387
void UndoPalette::undoClicked()
388
{
389
        emit undo(1);
390
}
391
 
392
void UndoPalette::redoClicked()
393
{
394
        emit redo(1);
395
}
396
 
397
void UndoPalette::undoListClicked(int i)
398
{
1190 tsoots 399
        if (i == currentSelection || (i == 0 && undoList->count() == 1))
1111 tsoots 400
                return;
401
        if (i > currentSelection)
402
                emit redo(i - currentSelection);
403
        else if (i < currentSelection)
404
                emit undo(currentSelection - i);
405
        currentSelection = i;
406
        updateList();
407
}
408
 
1443 tsoots 409
void UndoPalette::objectCheckBoxClicked(bool on)
410
{
411
        emit objectMode(on);
412
}
413
 
1200 tsoots 414
void UndoPalette::showToolTip(QListBoxItem *i)
415
{
416
        UndoItem *item = dynamic_cast<UndoItem*>(i);
417
        if (item)
418
        {
419
                QString tip = item->getDescription();
420
                if (tip != 0)
421
                  QToolTip::add(undoList, tip);
422
        }
423
        else
424
                removeToolTip();
425
}
426
 
427
void UndoPalette::removeToolTip()
428
{
429
        QToolTip::remove(undoList);
430
}
431
 
1111 tsoots 432
UndoPalette::~UndoPalette()
433
{
3923 tsoots 434
 
1111 tsoots 435
}
1142 tsoots 436
 
437
/*** UndoPalette::UndoItem ****************************************************/
438
 
439
UndoPalette::UndoItem::UndoItem() : QListBoxItem()
440
{
441
        target = "";
442
        action = "";
1200 tsoots 443
        description = "";
1250 tsoots 444
        targetpixmap = NULL;
445
        actionpixmap = NULL;
5950 tsoots 446
        isUndoAction_ = true;
1142 tsoots 447
}
448
 
449
UndoPalette::UndoItem::UndoItem(const UndoItem &another) : QListBoxItem()
450
{
451
        target = another.target;
452
        action = another.action;
1200 tsoots 453
        description = another.description;
1250 tsoots 454
        targetpixmap = another.targetpixmap;
455
        actionpixmap = another.actionpixmap;
5950 tsoots 456
        isUndoAction_ = another.isUndoAction_;
1142 tsoots 457
}
458
 
1250 tsoots 459
UndoPalette::UndoItem::UndoItem(const QString &targetName,
460
                                const QString &actionName,
461
                                const QString &actionDescription,
462
                                QPixmap *targetPixmap,
5950 tsoots 463
                                QPixmap *actionPixmap,
464
                                bool isUndoAction)
465
: QListBoxItem(),
466
targetpixmap(targetPixmap),
467
actionpixmap(actionPixmap),
468
target(targetName),
469
action(actionName),
470
description(actionDescription),
471
isUndoAction_(isUndoAction)
1142 tsoots 472
{
5950 tsoots 473
 
1142 tsoots 474
}
475
 
476
void UndoPalette::UndoItem::paint(QPainter *painter)
477
{
1250 tsoots 478
        if (targetpixmap)
479
                painter->drawPixmap(5, 5, *targetpixmap);
480
        if (actionpixmap)
481
                painter->drawPixmap(5, 5, *actionpixmap);
5950 tsoots 482
        QPen  p = QPen(painter->pen());
1167 tsoots 483
        QFont f = QFont(painter->font());
1173 tsoots 484
        painter->setFont(f);
5950 tsoots 485
        if (isUndoAction_)
486
        {
487
                p.setColor(QColor("black"));
488
                f.setItalic(false);
489
        }
490
        else
491
        {
492
                p.setColor(QColor("darkGray"));
493
                f.setItalic(true);
494
        }
495
        painter->setPen(p);
496
        painter->setFont(f);
1173 tsoots 497
        painter->drawText(32, QFontMetrics(f).height(), target);
1167 tsoots 498
        painter->drawText(32, (2 * QFontMetrics(painter->font()).height()), action);
1142 tsoots 499
}
500
 
1167 tsoots 501
int UndoPalette::UndoItem::height(const QListBox *lb) const
1142 tsoots 502
{
1167 tsoots 503
        if (lb)
1204 tsoots 504
        {
505
                int fontHeight = 2 * QFontMetrics(lb->font()).height();
1250 tsoots 506
                if (actionpixmap)
1254 tsoots 507
                        return fontHeight > (10 + actionpixmap->height()) ?
508
                   fontHeight : (10 + actionpixmap->height());
1250 tsoots 509
                else if (targetpixmap)
1254 tsoots 510
                        return fontHeight > (10 + targetpixmap->height()) ?
511
                   fontHeight : (10 + targetpixmap->height());
1204 tsoots 512
                else
513
                        return fontHeight;
514
        }
1167 tsoots 515
        else
516
                return 0;
1142 tsoots 517
}
518
 
1167 tsoots 519
int UndoPalette::UndoItem::width(const QListBox *lb) const
1142 tsoots 520
{
1167 tsoots 521
        if (lb)
522
                return target.length() > action.length() ?
1250 tsoots 523
               39 + QFontMetrics(lb->font()).width(target) :
524
               39 + QFontMetrics(lb->font()).width(action);
1167 tsoots 525
        else
526
                return 0;
1142 tsoots 527
}
528
 
1200 tsoots 529
QString UndoPalette::UndoItem::getDescription()
530
{
531
  return description;
532
}
533
 
5950 tsoots 534
bool UndoPalette::UndoItem::isUndoAction()
535
{
536
        return isUndoAction_;
537
}
538
 
539
void UndoPalette::UndoItem::setUndoAction(bool isUndo)
540
{
541
        isUndoAction_ = isUndo;
542
}
543
 
1142 tsoots 544
UndoPalette::UndoItem::~UndoItem()
545
{
1443 tsoots 546
 
1142 tsoots 547
}