summaryrefslogtreecommitdiffstats
path: root/src/widgets/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/util')
-rw-r--r--src/widgets/util/qsystemtrayicon.cpp117
-rw-r--r--src/widgets/util/qsystemtrayicon.h1
-rw-r--r--src/widgets/util/qsystemtrayicon_p.h19
-rw-r--r--src/widgets/util/qsystemtrayicon_qpa.cpp17
-rw-r--r--src/widgets/util/qsystemtrayicon_win.cpp57
-rw-r--r--src/widgets/util/qsystemtrayicon_x11.cpp17
-rw-r--r--src/widgets/util/qundostack.cpp156
-rw-r--r--src/widgets/util/qundostack.h3
-rw-r--r--src/widgets/util/qundostack_p.h3
9 files changed, 254 insertions, 136 deletions
diff --git a/src/widgets/util/qsystemtrayicon.cpp b/src/widgets/util/qsystemtrayicon.cpp
index 630524aadb..225c47052b 100644
--- a/src/widgets/util/qsystemtrayicon.cpp
+++ b/src/widgets/util/qsystemtrayicon.cpp
@@ -59,6 +59,25 @@
QT_BEGIN_NAMESPACE
+static QIcon messageIcon2qIcon(QSystemTrayIcon::MessageIcon icon)
+{
+ QStyle::StandardPixmap stdIcon = QStyle::SP_CustomBase; // silence gcc 4.9.0 about uninited variable
+ switch (icon) {
+ case QSystemTrayIcon::Information:
+ stdIcon = QStyle::SP_MessageBoxInformation;
+ break;
+ case QSystemTrayIcon::Warning:
+ stdIcon = QStyle::SP_MessageBoxWarning;
+ break;
+ case QSystemTrayIcon::Critical:
+ stdIcon = QStyle::SP_MessageBoxCritical;
+ break;
+ case QSystemTrayIcon::NoIcon:
+ return QIcon();
+ }
+ return QApplication::style()->standardIcon(stdIcon);
+}
+
/*!
\class QSystemTrayIcon
\brief The QSystemTrayIcon class provides an icon for an application in the system tray.
@@ -382,11 +401,29 @@ bool QSystemTrayIcon::supportsMessages()
\sa show(), supportsMessages()
*/
void QSystemTrayIcon::showMessage(const QString& title, const QString& msg,
- QSystemTrayIcon::MessageIcon icon, int msecs)
+ QSystemTrayIcon::MessageIcon msgIcon, int msecs)
{
Q_D(QSystemTrayIcon);
if (d->visible)
- d->showMessage_sys(title, msg, icon, msecs);
+ d->showMessage_sys(title, msg, messageIcon2qIcon(msgIcon), msgIcon, msecs);
+}
+
+/*!
+ \fn void QSystemTrayIcon::showMessage(const QString &title, const QString &message, const QIcon &icon, int millisecondsTimeoutHint)
+
+ \overload showMessage()
+
+ Shows a balloon message for the entry with the given \a title, \a message,
+ and custom icon \a icon for the time specified in \a millisecondsTimeoutHint.
+
+ \since 5.9
+*/
+void QSystemTrayIcon::showMessage(const QString &title, const QString &msg,
+ const QIcon &icon, int msecs)
+{
+ Q_D(QSystemTrayIcon);
+ if (d->visible)
+ d->showMessage_sys(title, msg, icon, QSystemTrayIcon::NoIcon, msecs);
}
void QSystemTrayIconPrivate::_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason reason)
@@ -398,9 +435,9 @@ void QSystemTrayIconPrivate::_q_emitActivated(QPlatformSystemTrayIcon::Activatio
//////////////////////////////////////////////////////////////////////
static QBalloonTip *theSolitaryBalloonTip = 0;
-void QBalloonTip::showBalloon(QSystemTrayIcon::MessageIcon icon, const QString& title,
- const QString& message, QSystemTrayIcon *trayIcon,
- const QPoint& pos, int timeout, bool showArrow)
+void QBalloonTip::showBalloon(const QIcon &icon, const QString &title,
+ const QString &message, QSystemTrayIcon *trayIcon,
+ const QPoint &pos, int timeout, bool showArrow)
{
hideBalloon();
if (message.isEmpty() && title.isEmpty())
@@ -434,8 +471,8 @@ bool QBalloonTip::isBalloonVisible()
return theSolitaryBalloonTip;
}
-QBalloonTip::QBalloonTip(QSystemTrayIcon::MessageIcon icon, const QString& title,
- const QString& message, QSystemTrayIcon *ti)
+QBalloonTip::QBalloonTip(const QIcon &icon, const QString &title,
+ const QString &message, QSystemTrayIcon *ti)
: QWidget(0, Qt::ToolTip),
trayIcon(ti),
timerId(-1),
@@ -485,26 +522,10 @@ QBalloonTip::QBalloonTip(QSystemTrayIcon::MessageIcon icon, const QString& title
msgLabel->setFixedSize(limit, msgLabel->heightForWidth(limit));
}
- QIcon si;
- switch (icon) {
- case QSystemTrayIcon::Warning:
- si = style()->standardIcon(QStyle::SP_MessageBoxWarning);
- break;
- case QSystemTrayIcon::Critical:
- si = style()->standardIcon(QStyle::SP_MessageBoxCritical);
- break;
- case QSystemTrayIcon::Information:
- si = style()->standardIcon(QStyle::SP_MessageBoxInformation);
- break;
- case QSystemTrayIcon::NoIcon:
- default:
- break;
- }
-
QGridLayout *layout = new QGridLayout;
- if (!si.isNull()) {
+ if (!icon.isNull()) {
QLabel *iconLabel = new QLabel;
- iconLabel->setPixmap(si.pixmap(iconSize, iconSize));
+ iconLabel->setPixmap(icon.pixmap(iconSize, iconSize));
iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
iconLabel->setMargin(2);
layout->addWidget(iconLabel, 0, 0);
@@ -681,52 +702,6 @@ void QSystemTrayIconPrivate::remove_sys_qpa()
qpa_sys->cleanup();
}
-QRect QSystemTrayIconPrivate::geometry_sys_qpa() const
-{
- return qpa_sys->geometry();
-}
-
-void QSystemTrayIconPrivate::updateIcon_sys_qpa()
-{
- qpa_sys->updateIcon(icon);
-}
-
-void QSystemTrayIconPrivate::updateMenu_sys_qpa()
-{
- if (menu) {
- addPlatformMenu(menu);
- qpa_sys->updateMenu(menu->platformMenu());
- }
-}
-
-void QSystemTrayIconPrivate::updateToolTip_sys_qpa()
-{
- qpa_sys->updateToolTip(toolTip);
-}
-
-void QSystemTrayIconPrivate::showMessage_sys_qpa(const QString &title,
- const QString &message,
- QSystemTrayIcon::MessageIcon icon,
- int msecs)
-{
- QIcon notificationIcon;
- switch (icon) {
- case QSystemTrayIcon::Information:
- notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation);
- break;
- case QSystemTrayIcon::Warning:
- notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning);
- break;
- case QSystemTrayIcon::Critical:
- notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical);
- break;
- default:
- break;
- }
- qpa_sys->showMessage(title, message, notificationIcon,
- static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs);
-}
-
void QSystemTrayIconPrivate::addPlatformMenu(QMenu *menu) const
{
if (menu->platformMenu())
diff --git a/src/widgets/util/qsystemtrayicon.h b/src/widgets/util/qsystemtrayicon.h
index fb238c92b0..918dd0478e 100644
--- a/src/widgets/util/qsystemtrayicon.h
+++ b/src/widgets/util/qsystemtrayicon.h
@@ -101,6 +101,7 @@ public Q_SLOTS:
void setVisible(bool visible);
inline void show() { setVisible(true); }
inline void hide() { setVisible(false); }
+ void showMessage(const QString &title, const QString &msg, const QIcon &icon, int msecs = 10000);
void showMessage(const QString &title, const QString &msg,
QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information, int msecs = 10000);
diff --git a/src/widgets/util/qsystemtrayicon_p.h b/src/widgets/util/qsystemtrayicon_p.h
index 79e824f4b7..3f5cab40be 100644
--- a/src/widgets/util/qsystemtrayicon_p.h
+++ b/src/widgets/util/qsystemtrayicon_p.h
@@ -84,7 +84,8 @@ public:
void updateToolTip_sys();
void updateMenu_sys();
QRect geometry_sys() const;
- void showMessage_sys(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int secs);
+ void showMessage_sys(const QString &title, const QString &msg, const QIcon &icon,
+ QSystemTrayIcon::MessageIcon msgIcon, int msecs);
static bool isSystemTrayAvailable_sys();
static bool supportsMessages_sys();
@@ -101,11 +102,7 @@ public:
private:
void install_sys_qpa();
void remove_sys_qpa();
- void updateIcon_sys_qpa();
- void updateToolTip_sys_qpa();
- void updateMenu_sys_qpa();
- QRect geometry_sys_qpa() const;
- void showMessage_sys_qpa(const QString &title, const QString &msg, QSystemTrayIcon::MessageIcon icon, int secs);
+
void addPlatformMenu(QMenu *menu) const;
};
@@ -113,16 +110,16 @@ class QBalloonTip : public QWidget
{
Q_OBJECT
public:
- static void showBalloon(QSystemTrayIcon::MessageIcon icon, const QString& title,
- const QString& msg, QSystemTrayIcon *trayIcon,
- const QPoint& pos, int timeout, bool showArrow = true);
+ static void showBalloon(const QIcon &icon, const QString &title,
+ const QString &msg, QSystemTrayIcon *trayIcon,
+ const QPoint &pos, int timeout, bool showArrow = true);
static void hideBalloon();
static bool isBalloonVisible();
static void updateBalloonPosition(const QPoint& pos);
private:
- QBalloonTip(QSystemTrayIcon::MessageIcon icon, const QString& title,
- const QString& msg, QSystemTrayIcon *trayIcon);
+ QBalloonTip(const QIcon &icon, const QString &title,
+ const QString &msg, QSystemTrayIcon *trayIcon);
~QBalloonTip();
void balloon(const QPoint&, int, bool);
diff --git a/src/widgets/util/qsystemtrayicon_qpa.cpp b/src/widgets/util/qsystemtrayicon_qpa.cpp
index 643f17a5fe..8399732a4a 100644
--- a/src/widgets/util/qsystemtrayicon_qpa.cpp
+++ b/src/widgets/util/qsystemtrayicon_qpa.cpp
@@ -76,7 +76,7 @@ void QSystemTrayIconPrivate::remove_sys()
QRect QSystemTrayIconPrivate::geometry_sys() const
{
if (qpa_sys)
- return geometry_sys_qpa();
+ return qpa_sys->geometry();
else
return QRect();
}
@@ -84,19 +84,21 @@ QRect QSystemTrayIconPrivate::geometry_sys() const
void QSystemTrayIconPrivate::updateIcon_sys()
{
if (qpa_sys)
- updateIcon_sys_qpa();
+ qpa_sys->updateIcon(icon);
}
void QSystemTrayIconPrivate::updateMenu_sys()
{
- if (qpa_sys)
- updateMenu_sys_qpa();
+ if (qpa_sys && menu) {
+ addPlatformMenu(menu);
+ qpa_sys->updateMenu(menu->platformMenu());
+ }
}
void QSystemTrayIconPrivate::updateToolTip_sys()
{
if (qpa_sys)
- updateToolTip_sys_qpa();
+ qpa_sys->updateToolTip(toolTip);
}
bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
@@ -118,10 +120,11 @@ bool QSystemTrayIconPrivate::supportsMessages_sys()
}
void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message,
- QSystemTrayIcon::MessageIcon icon, int msecs)
+ const QIcon &icon, QSystemTrayIcon::MessageIcon msgIcon, int msecs)
{
if (qpa_sys)
- showMessage_sys_qpa(title, message, icon, msecs);
+ qpa_sys->showMessage(title, message, icon,
+ static_cast<QPlatformSystemTrayIcon::MessageIcon>(msgIcon), msecs);
}
QT_END_NAMESPACE
diff --git a/src/widgets/util/qsystemtrayicon_win.cpp b/src/widgets/util/qsystemtrayicon_win.cpp
index 2da24e482b..638d2050fb 100644
--- a/src/widgets/util/qsystemtrayicon_win.cpp
+++ b/src/widgets/util/qsystemtrayicon_win.cpp
@@ -81,11 +81,14 @@ struct Q_NOTIFYICONIDENTIFIER {
# define NIN_BALLOONTIMEOUT (WM_USER + 4)
# define NIN_BALLOONUSERCLICK (WM_USER + 5)
# define NIF_SHOWTIP 0x00000080
+# define NIIF_LARGE_ICON 0x00000020
# define NOTIFYICON_VERSION_4 4
#endif
#define Q_MSGFLT_ALLOW 1
+Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &);
+
typedef HRESULT (WINAPI *PtrShell_NotifyIconGetRect)(const Q_NOTIFYICONIDENTIFIER* identifier, RECT* iconLocation);
typedef BOOL (WINAPI *PtrChangeWindowMessageFilter)(UINT message, DWORD dwFlag);
typedef BOOL (WINAPI *PtrChangeWindowMessageFilterEx)(HWND hWnd, UINT message, DWORD action, void* pChangeFilterStruct);
@@ -107,7 +110,7 @@ public:
~QSystemTrayIconSys();
bool trayMessage(DWORD msg);
void setIconContents(NOTIFYICONDATA &data);
- bool showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs);
+ bool showMessage(const QString &title, const QString &message, const QIcon &icon, uint uSecs);
QRect findIconGeometry(UINT iconId);
HICON createIcon();
bool winEvent(MSG *m, long *result);
@@ -184,7 +187,7 @@ static inline HWND createTrayIconMessageWindow()
QSystemTrayIconSys::QSystemTrayIconSys(HWND hwnd, QSystemTrayIcon *object)
: m_hwnd(hwnd), hIcon(0), q(object)
- , notifyIconSize(NOTIFYICONDATA_V2_SIZE), version(NOTIFYICON_VERSION)
+ , notifyIconSize(sizeof(NOTIFYICONDATA)), version(NOTIFYICON_VERSION_4)
, ignoreNextMouseRelease(false)
{
@@ -237,11 +240,7 @@ void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd)
qStringToLimitedWCharArray(tip, tnd.szTip, sizeof(tnd.szTip)/sizeof(wchar_t));
}
-#ifndef NIIF_LARGE_ICON
-# define NIIF_LARGE_ICON 0x00000020
-#endif
-
-bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs)
+bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, const QIcon &icon, uint uSecs)
{
NOTIFYICONDATA tnd;
memset(&tnd, 0, notifyIconSize);
@@ -249,23 +248,32 @@ bool QSystemTrayIconSys::showMessage(const QString &title, const QString &messag
qStringToLimitedWCharArray(title, tnd.szInfoTitle, 64);
tnd.uID = q_uNOTIFYICONID;
- switch (type) {
- case QSystemTrayIcon::Information:
+ tnd.dwInfoFlags = NIIF_USER;
+
+ HICON *phIcon = &tnd.hIcon;
+ QSize size(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
+ if (version == NOTIFYICON_VERSION_4) {
+ const QSize largeIcon(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
+ QSize more = icon.actualSize(largeIcon);
+ if (more.height() > (largeIcon.height() * 3/4) || more.width() > (largeIcon.width() * 3/4)) {
+ tnd.dwInfoFlags |= NIIF_LARGE_ICON;
+ size = largeIcon;
+ }
+ phIcon = &tnd.hBalloonIcon;
+ }
+ QPixmap pm = icon.pixmap(size);
+ if (pm.isNull()) {
tnd.dwInfoFlags = NIIF_INFO;
- break;
- case QSystemTrayIcon::Warning:
- tnd.dwInfoFlags = NIIF_WARNING;
- break;
- case QSystemTrayIcon::Critical:
- tnd.dwInfoFlags = NIIF_ERROR;
- break;
- case QSystemTrayIcon::NoIcon:
- tnd.dwInfoFlags = hIcon ? NIIF_USER : NIIF_NONE;
- break;
+ } else {
+ if (pm.size() != size) {
+ qWarning("QSystemTrayIcon::showMessage: Wrong icon size (%dx%d), please add standard one: %dx%d",
+ pm.size().width(), pm.size().height(), size.width(), size.height());
+ pm = pm.scaled(size, Qt::IgnoreAspectRatio);
+ }
+ *phIcon = qt_pixmapToWinHICON(pm);
}
- if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA)
- tnd.dwInfoFlags |= NIIF_LARGE_ICON;
tnd.cbSize = notifyIconSize;
+ tnd.uVersion = version;
tnd.hWnd = m_hwnd;
tnd.uTimeout = uSecs;
tnd.uFlags = NIF_INFO | NIF_SHOWTIP;
@@ -296,8 +304,6 @@ bool QSystemTrayIconSys::trayMessage(DWORD msg)
return success;
}
-Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &);
-
HICON QSystemTrayIconSys::createIcon()
{
const HICON oldIcon = hIcon;
@@ -509,7 +515,8 @@ QRect QSystemTrayIconSys::findIconGeometry(UINT iconId)
void QSystemTrayIconPrivate::showMessage_sys(const QString &title,
const QString &messageIn,
- QSystemTrayIcon::MessageIcon type,
+ const QIcon &icon,
+ QSystemTrayIcon::MessageIcon,
int timeOut)
{
if (!sys || !allowsMessages())
@@ -522,7 +529,7 @@ void QSystemTrayIconPrivate::showMessage_sys(const QString &title,
if (message.isEmpty() && !title.isEmpty())
message.append(QLatin1Char(' '));
- sys->showMessage(title, message, type, uSecs);
+ sys->showMessage(title, message, icon, uSecs);
}
QRect QSystemTrayIconPrivate::geometry_sys() const
diff --git a/src/widgets/util/qsystemtrayicon_x11.cpp b/src/widgets/util/qsystemtrayicon_x11.cpp
index ea0604b6ed..20ab0f6377 100644
--- a/src/widgets/util/qsystemtrayicon_x11.cpp
+++ b/src/widgets/util/qsystemtrayicon_x11.cpp
@@ -284,7 +284,7 @@ void QSystemTrayIconPrivate::install_sys()
QRect QSystemTrayIconPrivate::geometry_sys() const
{
if (qpa_sys)
- return geometry_sys_qpa();
+ return qpa_sys->geometry();
if (!sys)
return QRect();
return sys->globalGeometry();
@@ -307,7 +307,7 @@ void QSystemTrayIconPrivate::remove_sys()
void QSystemTrayIconPrivate::updateIcon_sys()
{
if (qpa_sys) {
- updateIcon_sys_qpa();
+ qpa_sys->updateIcon(icon);
return;
}
if (sys)
@@ -316,14 +316,16 @@ void QSystemTrayIconPrivate::updateIcon_sys()
void QSystemTrayIconPrivate::updateMenu_sys()
{
- if (qpa_sys)
- updateMenu_sys_qpa();
+ if (qpa_sys && menu) {
+ addPlatformMenu(menu);
+ qpa_sys->updateMenu(menu->platformMenu());
+ }
}
void QSystemTrayIconPrivate::updateToolTip_sys()
{
if (qpa_sys) {
- updateToolTip_sys_qpa();
+ qpa_sys->updateToolTip(toolTip);
return;
}
if (!sys)
@@ -357,10 +359,11 @@ bool QSystemTrayIconPrivate::supportsMessages_sys()
}
void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message,
- QSystemTrayIcon::MessageIcon icon, int msecs)
+ const QIcon &icon, QSystemTrayIcon::MessageIcon msgIcon, int msecs)
{
if (qpa_sys) {
- showMessage_sys_qpa(title, message, icon, msecs);
+ qpa_sys->showMessage(title, message, icon,
+ static_cast<QPlatformSystemTrayIcon::MessageIcon>(msgIcon), msecs);
return;
}
if (!sys)
diff --git a/src/widgets/util/qundostack.cpp b/src/widgets/util/qundostack.cpp
index 033d4e9e05..3d21320655 100644
--- a/src/widgets/util/qundostack.cpp
+++ b/src/widgets/util/qundostack.cpp
@@ -145,6 +145,36 @@ QUndoCommand::~QUndoCommand()
}
/*!
+ \since 5.9
+
+ Returns whether the command is obsolete.
+
+ The boolean is used for the automatic removal of commands that are not necessary in the
+ stack anymore. The isObsolete function is checked in the functions QUndoStack::push(),
+ QUndoStack::undo(), QUndoStack::redo(), and QUndoStack::setIndex().
+
+ \sa setObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo()
+*/
+
+bool QUndoCommand::isObsolete() const
+{
+ return d->obsolete;
+}
+
+/*!
+ \since 5.9
+
+ Sets whether the command is obsolete.
+
+ \sa isObsolete(), mergeWith(), QUndoStack::push(), QUndoStack::undo(), QUndoStack::redo()
+*/
+
+void QUndoCommand::setObsolete(bool obsolete)
+{
+ d->obsolete = obsolete;
+}
+
+/*!
Returns the ID of this command.
A command ID is used in command compression. It must be an integer unique to
@@ -390,6 +420,28 @@ const QUndoCommand *QUndoCommand::child(int index) const
and to update the document's title to reflect that it contains unsaved
changes.
+ \section1 Obsolete Commands
+
+ QUndoStack is able to delete commands from the stack if the command is no
+ longer needed. One example may be to delete a command when two commands are
+ merged together in such a way that the merged command has no function. This
+ can be seen with move commands where the user moves their mouse to one part
+ of the screen and then moves it to the original position. The merged command
+ results in a mouse movement of 0. This command can be deleted since it serves
+ no purpose. Another example is with networking commands that fail due to connection
+ issues. In this case, the command is to be removed from the stack because the redo()
+ and undo() functions have no function since there was connection issues.
+
+ A command can be marked obsolete with the QUndoCommand::setObsolete() function.
+ The QUndoCommand::isObsolete() flag is checked in QUndoStack::push(),
+ QUndoStack::undo(), QUndoStack::redo(), and QUndoStack::setIndex() after calling
+ QUndoCommand::undo(), QUndoCommand::redo() and QUndoCommand:mergeWith() where
+ applicable.
+
+ If a command is set obsolete and the clean index is greater than or equal to the
+ current command index, then the clean index will be reset when the command is
+ deleted from the stack.
+
\sa QUndoCommand, QUndoView
*/
@@ -563,6 +615,11 @@ void QUndoStack::clear()
commands by calling QUndoCommand::mergeWith() on the most recently executed
command. If QUndoCommand::mergeWith() returns \c true, \a cmd is deleted.
+ After calling QUndoCommand::redo() and, if applicable, QUndoCommand::mergeWith(),
+ QUndoCommand::isObsolete() will be called for \a cmd or the merged command.
+ If QUndoCommand::isObsolete() returns \c true, then \a cmd or the merged command
+ will be deleted from the stack.
+
In all other cases \a cmd is simply pushed on the stack.
If commands were undone before \a cmd was pushed, the current command and
@@ -580,7 +637,8 @@ void QUndoStack::clear()
void QUndoStack::push(QUndoCommand *cmd)
{
Q_D(QUndoStack);
- cmd->redo();
+ if (!cmd->isObsolete())
+ cmd->redo();
bool macro = !d->macro_stack.isEmpty();
@@ -605,13 +663,25 @@ void QUndoStack::push(QUndoCommand *cmd)
if (try_merge && cur->mergeWith(cmd)) {
delete cmd;
- if (!macro) {
- emit indexChanged(d->index);
- emit canUndoChanged(canUndo());
- emit undoTextChanged(undoText());
- emit canRedoChanged(canRedo());
- emit redoTextChanged(redoText());
+
+ if (macro) {
+ if (cur->isObsolete())
+ delete d->macro_stack.constLast()->d->child_list.takeLast();
+ } else {
+ if (cur->isObsolete()) {
+ delete d->command_list.takeLast();
+
+ d->setIndex(d->index - 1, false);
+ } else {
+ emit indexChanged(d->index);
+ emit canUndoChanged(canUndo());
+ emit undoTextChanged(undoText());
+ emit canRedoChanged(canRedo());
+ emit redoTextChanged(redoText());
+ }
}
+ } else if (cmd->isObsolete()) {
+ delete cmd; // command should be deleted and NOT added to the stack
} else {
if (macro) {
d->macro_stack.constLast()->d->child_list.append(cmd);
@@ -712,6 +782,11 @@ int QUndoStack::cleanIndex() const
If the stack is empty, or if the bottom command on the stack has already been
undone, this function does nothing.
+ After the command is undone, if QUndoCommand::isObsolete() returns \c true,
+ then the command will be deleted from the stack. Additionally, if the clean
+ index is greater than or equal to the current command index, then the clean
+ index is reset.
+
\sa redo(), index()
*/
@@ -727,7 +802,18 @@ void QUndoStack::undo()
}
int idx = d->index - 1;
- d->command_list.at(idx)->undo();
+ QUndoCommand *cmd = d->command_list.at(idx);
+
+ if (!cmd->isObsolete())
+ cmd->undo();
+
+ if (cmd->isObsolete()) { // A separate check is done b/c the undo command may set obsolete flag
+ delete d->command_list.takeAt(idx);
+
+ if (d->clean_index > idx)
+ resetClean();
+ }
+
d->setIndex(idx, false);
}
@@ -738,6 +824,11 @@ void QUndoStack::undo()
If the stack is empty, or if the top command on the stack has already been
redone, this function does nothing.
+ If QUndoCommand::isObsolete() returns true for the current command, then
+ the command will be deleted from the stack. Additionally, if the clean
+ index is greater than or equal to the current command index, then the clean
+ index is reset.
+
\sa undo(), index()
*/
@@ -752,8 +843,20 @@ void QUndoStack::redo()
return;
}
- d->command_list.at(d->index)->redo();
- d->setIndex(d->index + 1, false);
+ int idx = d->index;
+ QUndoCommand *cmd = d->command_list.at(idx);
+
+ if (!cmd->isObsolete())
+ cmd->redo(); // A separate check is done b/c the undo command may set obsolete flag
+
+ if (cmd->isObsolete()) {
+ delete d->command_list.takeAt(idx);
+
+ if (d->clean_index > idx)
+ resetClean();
+ } else {
+ d->setIndex(d->index + 1, false);
+ }
}
/*!
@@ -805,10 +908,35 @@ void QUndoStack::setIndex(int idx)
idx = d->command_list.size();
int i = d->index;
- while (i < idx)
- d->command_list.at(i++)->redo();
- while (i > idx)
- d->command_list.at(--i)->undo();
+ while (i < idx) {
+ QUndoCommand *cmd = d->command_list.at(i);
+
+ if (!cmd->isObsolete())
+ cmd->redo(); // A separate check is done b/c the undo command may set obsolete flag
+
+ if (cmd->isObsolete()) {
+ delete d->command_list.takeAt(i);
+
+ if (d->clean_index > i)
+ resetClean();
+
+ idx--; // Subtract from idx because we removed a command
+ } else {
+ i++;
+ }
+ }
+
+ while (i > idx) {
+ QUndoCommand *cmd = d->command_list.at(--i);
+
+ cmd->undo();
+ if (cmd->isObsolete()) {
+ delete d->command_list.takeAt(i);
+
+ if (d->clean_index > i)
+ resetClean();
+ }
+ }
d->setIndex(idx, false);
}
diff --git a/src/widgets/util/qundostack.h b/src/widgets/util/qundostack.h
index ca918b0618..2a8f4decb6 100644
--- a/src/widgets/util/qundostack.h
+++ b/src/widgets/util/qundostack.h
@@ -69,6 +69,9 @@ public:
QString actionText() const;
void setText(const QString &text);
+ bool isObsolete() const;
+ void setObsolete(bool obsolete);
+
virtual int id() const;
virtual bool mergeWith(const QUndoCommand *other);
diff --git a/src/widgets/util/qundostack_p.h b/src/widgets/util/qundostack_p.h
index 1bfe992426..e92a1fe620 100644
--- a/src/widgets/util/qundostack_p.h
+++ b/src/widgets/util/qundostack_p.h
@@ -66,11 +66,12 @@ class QUndoGroup;
class QUndoCommandPrivate
{
public:
- QUndoCommandPrivate() : id(-1) {}
+ QUndoCommandPrivate() : id(-1), obsolete(false) {}
QList<QUndoCommand*> child_list;
QString text;
QString actionText;
int id;
+ bool obsolete;
};
#ifndef QT_NO_UNDOSTACK