Subversion Repositories Scribus

Rev

Rev 24381 | 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.,                                       *
18122 mrdocs 24
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
1111 tsoots 25
 ***************************************************************************/
16546 jghali 26
#include <QCheckBox>
10859 cbradney 27
#include <QDebug>
28
#include <QEvent>
16546 jghali 29
#include <QPainter>
10223 cbradney 30
#include <QPushButton>
10192 cbradney 31
#include <QVBoxLayout>
10483 subik 32
 
20185 craig 33
#include "iconmanager.h"
2856 cbradney 34
#include "prefsmanager.h"
20185 craig 35
#include "scraction.h"
5781 cbradney 36
#include "scribuscore.h"
14509 cbradney 37
#include "ui/scmwmenumanager.h"
20185 craig 38
#include "undogui.h"
1111 tsoots 39
 
20185 craig 40
 
18194 fschmid 41
UndoGui::UndoGui(QWidget* parent, const char* name, Qt::WindowFlags f) : ScDockPalette(parent, name, f)
1111 tsoots 42
{
22058 craig 43
        languageChange();
1111 tsoots 44
}
45
 
20257 jghali 46
void UndoGui::languageChange()
47
{
48
        setWindowTitle( tr("Action History"));
49
}
50
 
1111 tsoots 51
/*** UndoWidget ***************************************************************/
52
 
18194 fschmid 53
UndoWidget::UndoWidget(QWidget* parent, const char* name) : UndoGui(parent, name)
1111 tsoots 54
{
22904 jghali 55
        auto &actions = ScCore->primaryMainWindow()->scrActions;
56
        auto menuManager = ScCore->primaryMainWindow()->scrMenuMgr;
57
 
1288 cbradney 58
        //Scribus action based toolbar button construction
22904 jghali 59
        parent->addAction(actions["editUndoAction"]);
60
        parent->addAction(actions["editRedoAction"]);
11292 subik 61
 
22904 jghali 62
        menuManager->createMenu("undoButtonMenu", "undoButtonMenu");
63
        menuManager->createMenu("redoButtonMenu", "redoButtonMenu");
64
        undoMenu = menuManager->undoMenu();
65
        redoMenu = menuManager->redoMenu();
66
 
67
        parent->addAction(actions["editCut"]);
68
        parent->addAction(actions["editCopy"]);
69
        parent->addAction(actions["editPaste"]);
70
 
22601 craig 71
        connect(undoMenu, SIGNAL(triggered(QAction*)), this, SLOT(undoMenuClicked(QAction*)));
72
        connect(redoMenu, SIGNAL(triggered(QAction*)), this, SLOT(redoMenuClicked(QAction*)));
1111 tsoots 73
}
74
 
75
void UndoWidget::clear()
76
{
77
        undoMenu->clear();
78
        undoItems.clear();
1288 cbradney 79
        //Scribus disable
5781 cbradney 80
        ScCore->primaryMainWindow()->scrActions["editUndoAction"]->setEnabled(false);
1111 tsoots 81
        redoMenu->clear();
82
        redoItems.clear();
1288 cbradney 83
        //Scribus disable;
5781 cbradney 84
        ScCore->primaryMainWindow()->scrActions["editRedoAction"]->setEnabled(false);
1111 tsoots 85
}
86
 
87
void UndoWidget::undoClicked()
88
{
22601 craig 89
        if (!undoItems.empty())
1111 tsoots 90
                emit undo(1);
91
}
92
 
93
void UndoWidget::redoClicked()
94
{
22601 craig 95
        if (!redoItems.empty())
1111 tsoots 96
                emit redo(1);
97
}
98
 
10592 fschmid 99
void UndoWidget::undoMenuClicked(QAction *id)
1111 tsoots 100
{
10592 fschmid 101
        int steps = undoMenu->actions().indexOf(id) + 1;
1111 tsoots 102
        emit undo(steps);
103
}
104
 
10592 fschmid 105
void UndoWidget::redoMenuClicked(QAction *id)
1111 tsoots 106
{
10592 fschmid 107
        int steps = redoMenu->actions().indexOf(id) + 1;
1111 tsoots 108
        emit redo(steps);
109
}
110
 
1336 tsoots 111
void UndoWidget::insertUndoItem(UndoObject* target, UndoState* state)
1111 tsoots 112
{
21942 craig 113
        undoItems.insert(undoItems.begin(), QString( tr("%1: %2", "undo target: action (f.e. Text frame: Resize)")).arg(target->getUName(), state->getName()));
1457 tsoots 114
        clearRedo();
1111 tsoots 115
        updateUndoMenu();
1443 tsoots 116
        updateRedoMenu();
1111 tsoots 117
}
118
 
1336 tsoots 119
void UndoWidget::insertRedoItem(UndoObject* target, UndoState* state)
1111 tsoots 120
{
21942 craig 121
        redoItems.push_back(QString( tr("%1: %2", "undo target: action (f.e. Text frame: Resize)")).arg(target->getUName(), state->getName()));
1111 tsoots 122
        updateRedoMenu();
1443 tsoots 123
        updateUndoMenu();
1111 tsoots 124
}
125
 
1457 tsoots 126
void UndoWidget::clearRedo()
1111 tsoots 127
{
128
        redoItems.erase(redoItems.begin(), redoItems.end());
129
        updateRedoMenu();
130
}
131
 
132
void UndoWidget::updateUndoMenu()
133
{
134
        undoMenu->clear();
135
        for (uint i = 0; i < MENU_HEIGHT && i < undoItems.size(); ++i)
10592 fschmid 136
                undoMenu->addAction(undoItems[i]);
4985 cbradney 137
        updateUndoActions();
1111 tsoots 138
}
139
 
140
void UndoWidget::updateRedoMenu()
141
{
142
        redoMenu->clear();
143
        for (uint i = 0; i < MENU_HEIGHT && i < redoItems.size(); ++i)
10592 fschmid 144
                redoMenu->addAction(redoItems[i]);
4985 cbradney 145
        updateUndoActions();
146
}
147
 
148
void UndoWidget::updateUndoActions()
149
{
10592 fschmid 150
        ScCore->primaryMainWindow()->scrActions["editUndoAction"]->setEnabled(undoMenu->actions().count() != 0);
151
        ScCore->primaryMainWindow()->scrActions["editRedoAction"]->setEnabled(redoMenu->actions().count() != 0);
1111 tsoots 152
}
153
 
154
void UndoWidget::updateUndo(int steps)
155
{
156
        for (int i = 0; i < steps; ++i)
157
        {
158
                redoItems.insert(redoItems.begin(), undoItems[0]);
11292 subik 159
                undoItems.erase(undoItems.begin());
1111 tsoots 160
        }
161
        updateUndoMenu();
162
        updateRedoMenu();
163
}
164
 
165
void UndoWidget::updateRedo(int steps)
166
{
167
        for (int i = 0; i < steps; ++i)
168
        {
169
                undoItems.insert(undoItems.begin(), redoItems[0]);
11292 subik 170
                redoItems.erase(redoItems.begin());
1111 tsoots 171
        }
172
        updateUndoMenu();
173
        updateRedoMenu();
174
}
175
 
176
void UndoWidget::popBack()
177
{
22601 craig 178
        if (!undoItems.empty())
1197 tsoots 179
        {
180
                undoItems.erase(undoItems.end() - 1);
181
                updateUndoMenu();
182
        }
1111 tsoots 183
}
184
 
185
/*** UndoPalette **************************************************************/
186
 
18194 fschmid 187
UndoPalette::UndoPalette(QWidget* parent, const char* name) : UndoGui(parent, name)
1111 tsoots 188
{
17735 craig 189
        setObjectName(QString::fromLocal8Bit(name));
24325 jghali 190
        setMinimumSize( QSize(220, 240) );
17735 craig 191
        setSizePolicy( QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
192
 
193
        container = new QWidget(this);
194
        QVBoxLayout* layout = new QVBoxLayout(container);
24381 jghali 195
        layout->setContentsMargins(3, 3, 3, 3);
196
        layout->setSpacing(3);
10592 fschmid 197
        objectBox = new QCheckBox(this);
1111 tsoots 198
        layout->addWidget(objectBox);
199
 
10483 subik 200
        undoList = new QListWidget(this);
201
        undoList->setSelectionMode(QAbstractItemView::SingleSelection);
11292 subik 202
        undoList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
1111 tsoots 203
        layout->addWidget(undoList);
11292 subik 204
 
10592 fschmid 205
        QHBoxLayout* buttonLayout = new QHBoxLayout;
24381 jghali 206
        buttonLayout->setContentsMargins(0, 0, 0, 0);
207
        buttonLayout->setSpacing(6);
23054 craig 208
        undoButton = new QPushButton(IconManager::instance().loadPixmap("16/edit-undo.png"), "", this);
1111 tsoots 209
        buttonLayout->addWidget(undoButton);
23054 craig 210
        redoButton = new QPushButton(IconManager::instance().loadPixmap("16/edit-redo.png"), "", this);
2198 cbradney 211
        buttonLayout->addWidget(redoButton);
1441 cbradney 212
        //Save the translated key sequence - hopefully we get the translated one here!
10592 fschmid 213
        initialUndoKS = undoButton->shortcut();
214
        initialRedoKS = redoButton->shortcut();
1111 tsoots 215
        layout->addLayout(buttonLayout);
17735 craig 216
        setWidget(container);
1111 tsoots 217
 
1441 cbradney 218
        updateFromPrefs();
2198 cbradney 219
        languageChange();
23060 craig 220
        connect(&PrefsManager::instance(), SIGNAL(prefsChanged()), this, SLOT(updateFromPrefs()));
1111 tsoots 221
        connect(undoButton, SIGNAL(clicked()), this, SLOT(undoClicked()));
222
        connect(redoButton, SIGNAL(clicked()), this, SLOT(redoClicked()));
10483 subik 223
        connect(undoList, SIGNAL(currentRowChanged(int)), this, SLOT(undoListClicked(int)));
224
        connect(undoList, SIGNAL(itemEntered(QListWidgetItem*)), this, SLOT(showToolTip(QListWidgetItem*)));
225
        connect(undoList, SIGNAL(viewportEntered()), this, SLOT(removeToolTip()));
1443 tsoots 226
        connect(objectBox, SIGNAL(toggled(bool)), this, SLOT(objectCheckBoxClicked(bool)));
5781 cbradney 227
        connect(ScCore->primaryMainWindow()->scrActions["editActionMode"], SIGNAL(toggled(bool)),
1446 tsoots 228
                objectBox, SLOT(setChecked(bool)));
11292 subik 229
        connect(objectBox, SIGNAL(toggled(bool)),
10581 cbradney 230
                        ScCore->primaryMainWindow()->scrActions["editActionMode"], SLOT(setChecked(bool)));
1111 tsoots 231
}
232
 
233
void UndoPalette::clear()
234
{
10546 subik 235
        disconnect(undoList, SIGNAL(currentRowChanged(int)), this, SLOT(undoListClicked(int)));
1111 tsoots 236
        undoList->clear();
10483 subik 237
        undoList->addItem( tr("Initial State"));
1111 tsoots 238
        undoButton->setEnabled(false);
239
        redoButton->setEnabled(false);
10546 subik 240
        connect(undoList, SIGNAL(currentRowChanged(int)), this, SLOT(undoListClicked(int)));
1111 tsoots 241
}
242
 
1441 cbradney 243
void UndoPalette::updateFromPrefs()
244
{
10592 fschmid 245
        undoButton->setShortcut(ScCore->primaryMainWindow()->scrActions["editUndoAction"]->shortcut());
246
        redoButton->setShortcut(ScCore->primaryMainWindow()->scrActions["editRedoAction"]->shortcut());
1441 cbradney 247
}
248
 
10859 cbradney 249
void UndoPalette::changeEvent(QEvent *e)
250
{
251
        if (e->type() == QEvent::LanguageChange)
252
                languageChange();
10903 cbradney 253
        else
20257 jghali 254
                UndoGui::changeEvent(e);
10859 cbradney 255
}
256
 
2198 cbradney 257
void UndoPalette::languageChange()
258
{
20257 jghali 259
        UndoGui::languageChange();
260
 
13032 cbradney 261
        objectBox->setText( tr("Show Selected Object Only"));
2397 cbradney 262
        undoButton->setText( tr("&Undo"));
263
        redoButton->setText( tr("&Redo"));
12208 cbradney 264
 
265
        objectBox->setToolTip( "<qt>" + tr( "Show the action history for the selected item only. This changes the effect of the undo/redo buttons to act on the object or document." ) + "</qt>" );
266
        undoButton->setToolTip( "<qt>" + tr( "Undo the last action for either the current object or the document" ) + "</qt>");
267
        redoButton->setToolTip( "<qt>" + tr( "Redo the last action for either the current object or the document" ) + "</qt>");
2198 cbradney 268
}
269
 
1111 tsoots 270
void UndoPalette::insertUndoItem(UndoObject* target, UndoState* state)
271
{
1457 tsoots 272
        clearRedo();
10483 subik 273
        undoList->addItem(new UndoItem(target->getUName(), state->getName(),
1250 tsoots 274
                         state->getDescription(), target->getUPixmap(),
5950 tsoots 275
                         state->getPixmap(), true));
10483 subik 276
        currentSelection = undoList->count() - 1;
1111 tsoots 277
        updateList();
278
}
279
 
280
void UndoPalette::insertRedoItem(UndoObject* target, UndoState* state)
281
{
1190 tsoots 282
        if (undoList->count() == 1)
1443 tsoots 283
        {
10483 subik 284
                undoList->setCurrentItem(undoList->item(0));
1443 tsoots 285
                currentSelection = 0;
286
        }
10483 subik 287
        undoList->addItem(new UndoItem(target->getUName(), state->getName(),
1250 tsoots 288
                         state->getDescription(), target->getUPixmap(),
5950 tsoots 289
                         state->getPixmap(), false));
1111 tsoots 290
        updateList();
291
}
292
 
293
void UndoPalette::updateUndo(int steps)
294
{
10483 subik 295
        if (undoList->row(undoList->currentItem()) == currentSelection)
1111 tsoots 296
        {
297
                currentSelection -= steps;
298
                updateList();
299
        }
300
}
301
 
302
void UndoPalette::updateRedo(int steps)
303
{
10483 subik 304
        if (undoList->row(undoList->currentItem()) == currentSelection)
1111 tsoots 305
        {
306
                currentSelection += steps;
307
                updateList();
308
        }
309
}
310
 
311
void UndoPalette::popBack()
312
{
1197 tsoots 313
        if (undoList->count() > 1)
314
        {
10483 subik 315
                delete undoList->takeItem(0);
316
                currentSelection = undoList->count() - 1;
1197 tsoots 317
        }
1111 tsoots 318
}
319
 
320
void UndoPalette::updateList()
321
{
10483 subik 322
        undoList->setCurrentRow(currentSelection);
323
        redoButton->setEnabled(currentSelection < undoList->count() - 1);
1111 tsoots 324
        undoButton->setEnabled(currentSelection > 0);
10483 subik 325
        undoList->scrollToItem(undoList->item(currentSelection));
326
        for (int i = 0; i < undoList->count(); ++i)
5950 tsoots 327
        {
24730 jghali 328
                auto *item = dynamic_cast<UndoItem*>(undoList->item(i));
5950 tsoots 329
                if (!item)
330
                        continue;
331
 
332
                item->setUndoAction(currentSelection >= i);
333
        }
1111 tsoots 334
}
335
 
4985 cbradney 336
void UndoPalette::updateUndoActions()
337
{
338
        //ScMW->scrActions["editUndoAction"]->setEnabled(currentSelection > 0);
339
        //ScMW->scrActions["editRedoAction"]->setEnabled(currentSelection < undoList->numRows() - 1);
340
}
341
 
1457 tsoots 342
void UndoPalette::clearRedo()
1111 tsoots 343
{
10483 subik 344
        for (int i = (undoList->count() - 1); i > currentSelection; --i)
345
                delete undoList->takeItem(i);
1111 tsoots 346
}
347
 
348
void UndoPalette::undoClicked()
349
{
350
        emit undo(1);
351
}
352
 
353
void UndoPalette::redoClicked()
354
{
355
        emit redo(1);
356
}
357
 
358
void UndoPalette::undoListClicked(int i)
359
{
1190 tsoots 360
        if (i == currentSelection || (i == 0 && undoList->count() == 1))
1111 tsoots 361
                return;
362
        if (i > currentSelection)
363
                emit redo(i - currentSelection);
364
        else if (i < currentSelection)
365
                emit undo(currentSelection - i);
366
        currentSelection = i;
367
        updateList();
368
}
369
 
1443 tsoots 370
void UndoPalette::objectCheckBoxClicked(bool on)
371
{
372
        emit objectMode(on);
373
}
374
 
10483 subik 375
void UndoPalette::showToolTip(QListWidgetItem *i)
1200 tsoots 376
{
24730 jghali 377
        auto *item = dynamic_cast<UndoItem*>(i);
1200 tsoots 378
        if (item)
379
        {
380
                QString tip = item->getDescription();
11584 herm 381
                if (!tip.isNull()) /*TODO: Doesn't make sense! */
10397 cbradney 382
                  undoList->setToolTip(tip);
1200 tsoots 383
        }
384
        else
385
                removeToolTip();
386
}
387
 
388
void UndoPalette::removeToolTip()
389
{
10397 cbradney 390
        undoList->setToolTip("");
1200 tsoots 391
}
392
 
1142 tsoots 393
/*** UndoPalette::UndoItem ****************************************************/
394
 
24730 jghali 395
UndoPalette::UndoItem::UndoItem(const UndoItem &another)
396
        : m_targetPixmap(another.m_targetPixmap),
397
          m_actionPixmap(another.m_actionPixmap),
398
          m_target(another.m_target),
399
          m_action(another.m_action),
400
          m_description(another.m_description),
401
          m_isUndoAction(another.m_isUndoAction)
1142 tsoots 402
{
403
 
404
}
405
 
1250 tsoots 406
UndoPalette::UndoItem::UndoItem(const QString &targetName,
407
                                const QString &actionName,
408
                                const QString &actionDescription,
409
                                QPixmap *targetPixmap,
5950 tsoots 410
                                QPixmap *actionPixmap,
10548 subik 411
                                bool isUndoAction,
412
                                QListWidget * parent
413
                                                           )
414
        : QListWidgetItem(parent),
24730 jghali 415
        m_targetPixmap(targetPixmap),
416
        m_actionPixmap(actionPixmap),
417
        m_target(targetName),
418
        m_action(actionName),
419
        m_description(actionDescription),
420
        m_isUndoAction(isUndoAction)
1142 tsoots 421
{
11584 herm 422
        /*TODO: 16x16 is hardcoded, because images automatically scaled by QIcon are no longer recognizable
423
        would be better to have the icons designed for 16x16*/
24274 jghali 424
        if (targetPixmap)
22058 craig 425
        {
24274 jghali 426
                QPixmap pixmap;
427
                if (!targetPixmap->isNull())
428
                        pixmap = targetPixmap->scaled(16, 16);
429
                if (actionPixmap && !actionPixmap->isNull())
22058 craig 430
                {
11584 herm 431
                        QPainter p;
432
                        p.begin(&pixmap);
24274 jghali 433
                        p.drawPixmap(0,0, actionPixmap->scaled(16, 16));
11584 herm 434
                        p.end();
435
                }
436
                setIcon(pixmap);
437
        }
24274 jghali 438
        else
439
        {
440
                if (actionPixmap && !actionPixmap->isNull())
441
                        setIcon(actionPixmap->scaled(16, 16));
442
        }
21942 craig 443
        setText(tr("%1 - %2\n%3").arg(targetName, actionName, actionDescription));
1142 tsoots 444
}
445
 
24730 jghali 446
QString UndoPalette::UndoItem::getDescription() const
1200 tsoots 447
{
24730 jghali 448
  return m_description;
1200 tsoots 449
}
450
 
24730 jghali 451
bool UndoPalette::UndoItem::isUndoAction() const
5950 tsoots 452
{
24730 jghali 453
        return m_isUndoAction;
5950 tsoots 454
}
455
 
456
void UndoPalette::UndoItem::setUndoAction(bool isUndo)
457
{
24730 jghali 458
        m_isUndoAction = isUndo;
11584 herm 459
        QFont f = font();
24730 jghali 460
        f.setItalic(!m_isUndoAction);
11584 herm 461
        setFont(f);
5950 tsoots 462
}