File indexing completed on 2024-05-12 16:39:44
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> 0003 Copyright (C) 2005-2010 Jarosław Staniek <staniek@kde.org> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef KFORMEDITOR_COMMANDS_H 0022 #define KFORMEDITOR_COMMANDS_H 0023 0024 #include "kformdesigner_export.h" 0025 #include "utils.h" 0026 #include "objecttree.h" 0027 #include "form.h" 0028 0029 #include <QHash> 0030 #include <QVariant> 0031 0032 #include <kundo2command.h> 0033 0034 #include <QDebug> 0035 0036 class QPoint; 0037 class QStringList; 0038 class QDomElement; 0039 0040 namespace KFormDesigner 0041 { 0042 0043 class ObjectTreeItem; 0044 class Container; 0045 class Form; 0046 0047 //! Base class for KFormDesigner's commands 0048 class KFORMDESIGNER_EXPORT Command : public KUndo2Command 0049 { 0050 public: 0051 explicit Command(Command *parent = 0); 0052 0053 explicit Command(const QString &text, Command *parent = 0); 0054 0055 virtual ~Command(); 0056 0057 //! Reimplemented to support effect of blockRedoOnce(). 0058 virtual void redo() override; 0059 0060 //! Implement instead of redo(). 0061 virtual void execute() = 0; 0062 0063 virtual void debug() const; 0064 0065 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const Command &c); 0066 protected: 0067 //! Used to block execution of redo() once, on adding the command to the stack. 0068 void blockRedoOnce(); 0069 0070 friend class Form; 0071 bool m_blockRedoOnce; //!< Used to block redo() once 0072 }; 0073 0074 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0075 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const Command &c); 0076 0077 //! Command is used when changing a property for one or more widgets. 0078 class KFORMDESIGNER_EXPORT PropertyCommand : public Command 0079 { 0080 public: 0081 /*! @a oldValue is the old property value for selected widget. 0082 This enables reverting the change. @a value is the new property value. */ 0083 PropertyCommand(Form& form, const QByteArray &wname, const QVariant &oldValue, 0084 const QVariant &value, const QByteArray &propertyName, Command *parent = 0); 0085 0086 /*! @a oldValues is a QHash of the old property values for every widget, 0087 to allow reverting the change. @a value is the new property value. 0088 You can use the simpler constructor for a single widget. */ 0089 PropertyCommand(Form& form, const QHash<QByteArray, QVariant> &oldValues, 0090 const QVariant &value, const QByteArray &propertyName, Command *parent = 0); 0091 0092 virtual ~PropertyCommand(); 0093 0094 Form* form() const; 0095 0096 virtual int id() const override; 0097 0098 void setUniqueId(int id); 0099 0100 virtual void execute() override; 0101 0102 virtual void undo() override; 0103 0104 bool mergeWith(const KUndo2Command * command) override; 0105 0106 QByteArray propertyName() const; 0107 0108 QVariant value() const; 0109 0110 void setValue(const QVariant &value); 0111 0112 const QHash<QByteArray, QVariant>& oldValues() const; 0113 0114 //! @return old value if there is single value, otherwise null value. 0115 QVariant oldValue() const; 0116 0117 //! @return widget name in case when there is only one widget 0118 //! with changed property in this command 0119 /*! Otherwise empty value is returned. */ 0120 QByteArray widgetName() const; 0121 0122 virtual void debug() const override; 0123 0124 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PropertyCommand &c); 0125 protected: 0126 void init(); 0127 class Private; 0128 Private * const d; 0129 }; 0130 0131 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0132 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PropertyCommand &c); 0133 0134 //! Command used when moving multiples widgets at the same time, while holding Ctrl or Shift. 0135 /*! You need to supply a list of widget names, and the position of the cursor before moving. Use setPos() 0136 to tell the new cursor pos every time it changes.*/ 0137 class KFORMDESIGNER_EXPORT GeometryPropertyCommand : public Command 0138 { 0139 public: 0140 GeometryPropertyCommand(Form& form, const QStringList &names, 0141 const QPoint& oldPos, Command *parent = 0); 0142 0143 virtual ~GeometryPropertyCommand(); 0144 0145 virtual int id() const override; 0146 0147 virtual void execute() override; 0148 0149 virtual void undo() override; 0150 0151 void setPos(const QPoint& pos); 0152 0153 QPoint pos() const; 0154 0155 QPoint oldPos() const; 0156 0157 virtual void debug() const override; 0158 0159 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const GeometryPropertyCommand &c); 0160 protected: 0161 class Private; 0162 Private * const d; 0163 }; 0164 0165 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0166 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const GeometryPropertyCommand &c); 0167 0168 //! Command used when an "Align Widgets position" action is activated. 0169 /* You just need to give the list of widget names (the selected ones), and the 0170 type of alignment (see the enum for possible values). */ 0171 class KFORMDESIGNER_EXPORT AlignWidgetsCommand : public Command 0172 { 0173 public: 0174 AlignWidgetsCommand(Form &form, Form::WidgetAlignment alignment, const QWidgetList &list, 0175 Command *parent = 0); 0176 0177 virtual ~AlignWidgetsCommand(); 0178 0179 virtual int id() const override; 0180 0181 virtual void execute() override; 0182 0183 virtual void undo() override; 0184 0185 virtual void debug() const override; 0186 0187 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const AlignWidgetsCommand &c); 0188 protected: 0189 class Private; 0190 Private * const d; 0191 }; 0192 0193 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0194 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const AlignWidgetsCommand &c); 0195 0196 //! Command used when an "Adjust Widgets Size" action is activated. 0197 /*! You just need to give the list of widget names (the selected ones), 0198 and the type of size modification (see the enum for possible values). */ 0199 class KFORMDESIGNER_EXPORT AdjustSizeCommand : public Command 0200 { 0201 public: 0202 enum Adjustment { 0203 SizeToGrid, 0204 SizeToFit, 0205 SizeToSmallWidth, 0206 SizeToBigWidth, 0207 SizeToSmallHeight, 0208 SizeToBigHeight 0209 }; 0210 0211 AdjustSizeCommand(Form& form, Adjustment type, const QWidgetList &list, Command *parent = 0); 0212 0213 virtual ~AdjustSizeCommand(); 0214 0215 virtual int id() const override; 0216 0217 virtual void execute() override; 0218 0219 virtual void undo() override; 0220 0221 virtual void debug() const override; 0222 0223 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const AdjustSizeCommand &c); 0224 protected: 0225 QSize getSizeFromChildren(ObjectTreeItem *item); 0226 0227 protected: 0228 class Private; 0229 Private * const d; 0230 }; 0231 0232 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0233 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const AdjustSizeCommand &c); 0234 0235 //! Command used when switching the layout of a container. 0236 /*! It remembers the old pos of every widget inside the container. */ 0237 class KFORMDESIGNER_EXPORT LayoutPropertyCommand : public PropertyCommand 0238 { 0239 public: 0240 LayoutPropertyCommand(Form& form, const QByteArray &wname, 0241 const QVariant &oldValue, const QVariant &value, 0242 Command *parent = 0); 0243 0244 virtual ~LayoutPropertyCommand(); 0245 0246 virtual int id() const override; 0247 0248 virtual void execute() override; 0249 0250 virtual void undo() override; 0251 0252 virtual void debug() const override; 0253 0254 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const LayoutPropertyCommand &c); 0255 protected: 0256 class Private; 0257 Private * const d; 0258 }; 0259 0260 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0261 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const LayoutPropertyCommand &c); 0262 0263 //! Command used when inserting a widget using toolbar or menu. 0264 /*! You only have to give the parent Container and the widget pos. 0265 The other information is taken from the form. */ 0266 class KFORMDESIGNER_EXPORT InsertWidgetCommand : public Command 0267 { 0268 public: 0269 explicit InsertWidgetCommand(const Container& container, Command *parent = 0); 0270 0271 /*! This ctor allows to set explicit class name and position. 0272 Used for dropping widgets on the form surface. 0273 If \a namePrefix is empty, widget's unique name is constructed using 0274 hint for \a className (WidgetLibrary::namePrefix()), 0275 otherwise, \a namePrefix is used to generate widget's name. 0276 This allows e.g. inserting a widgets having name constructed using 0277 */ 0278 InsertWidgetCommand(const Container& container, const QByteArray& className, 0279 const QPoint& pos, const QByteArray& namePrefix = QByteArray(), 0280 Command *parent = 0); 0281 0282 virtual ~InsertWidgetCommand(); 0283 0284 virtual int id() const override; 0285 0286 virtual void execute() override; 0287 0288 virtual void undo() override; 0289 0290 //! @return inserted widget's name 0291 QByteArray widgetName() const; 0292 0293 virtual void debug() const override; 0294 0295 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InsertWidgetCommand &c); 0296 protected: 0297 void init(); 0298 0299 class Private; 0300 Private * const d; 0301 }; 0302 0303 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0304 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InsertWidgetCommand &c); 0305 0306 //! @todo add CopyWidgetCommand 0307 0308 //! Command used when pasting widgets. 0309 /*! You need to give the QDomDocument containing 0310 the widget(s) to paste, and optionally the point where to paste widgets. */ 0311 class KFORMDESIGNER_EXPORT PasteWidgetCommand : public Command 0312 { 0313 public: 0314 PasteWidgetCommand(const QDomDocument &domDoc, const Container& container, 0315 const QPoint& p = QPoint(), Command *parent = 0); 0316 0317 virtual ~PasteWidgetCommand(); 0318 0319 virtual int id() const override; 0320 0321 virtual void execute() override; 0322 0323 virtual void undo() override; 0324 0325 virtual void debug() const override; 0326 0327 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PasteWidgetCommand &c); 0328 0329 protected: 0330 /*! Internal function used to change the coordinates of a widget to \a newPos 0331 before pasting it (to paste it at the position of the contextual menu). It modifies 0332 the "geometry" property of the QDomElement representing the widget. */ 0333 void changePos(QDomElement &el, const QPoint &newPos); 0334 0335 /*! Internal function used to fix the coordinates of a widget before pasting it 0336 (to avoid having two widgets at the same position). It moves the widget by 0337 (10, 10) increment (several times if there are already pasted widgets at this position). */ 0338 void fixPos(QDomElement &el, Container *container); 0339 0340 void moveWidgetBy(QDomElement &el, Container *container, const QPoint &p); 0341 0342 /*! Internal function used to fix the names of the widgets before pasting them. 0343 It prevents from pasting a widget with 0344 the same name as an actual widget. The child widgets are also fixed recursively.\n 0345 If the name of the widget ends with a number (eg "QLineEdit1"), the new name is 0346 just incremented by one (eg becomes "QLineEdit2"). Otherwise, a "2" is just 0347 appended at the end of the name (eg "myWidget" becomes "myWidget2"). */ 0348 void fixNames(QDomElement &el); 0349 0350 protected: 0351 class Private; 0352 Private * const d; 0353 }; 0354 0355 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0356 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PasteWidgetCommand &c); 0357 0358 //! Command used when deleting widgets using the "Delete" menu item. 0359 /*! You need to give a QWidgetList of the selected widgets. */ 0360 class KFORMDESIGNER_EXPORT DeleteWidgetCommand : public Command 0361 { 0362 public: 0363 DeleteWidgetCommand(Form& form, const QWidgetList &list, Command *parent = 0); 0364 0365 virtual ~DeleteWidgetCommand(); 0366 0367 virtual int id() const override; 0368 0369 virtual void execute() override; 0370 0371 virtual void undo() override; 0372 0373 virtual void debug() const override; 0374 0375 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const DeleteWidgetCommand &c); 0376 protected: 0377 class Private; 0378 Private * const d; 0379 }; 0380 0381 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0382 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const DeleteWidgetCommand &c); 0383 0384 //! Command used when duplicating widgets. 0385 /*! You need to give a QWidgetList of the selected widgets. */ 0386 class KFORMDESIGNER_EXPORT DuplicateWidgetCommand : public Command 0387 { 0388 public: 0389 DuplicateWidgetCommand(const Container& container, const QWidgetList &list, 0390 const QPoint& copyToPoint, Command *parent = 0); 0391 0392 virtual ~DuplicateWidgetCommand(); 0393 0394 virtual int id() const override; 0395 0396 virtual void execute() override; 0397 0398 virtual void undo() override; 0399 0400 virtual void debug() const override; 0401 0402 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const DuplicateWidgetCommand &c); 0403 protected: 0404 class Private; 0405 Private * const d; 0406 }; 0407 0408 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0409 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const DuplicateWidgetCommand &c); 0410 0411 //! Command used when cutting widgets. 0412 /*! It is basically a DeleteWidgetCommand which also updates the clipboard contents. */ 0413 class KFORMDESIGNER_EXPORT CutWidgetCommand : public DeleteWidgetCommand 0414 { 0415 public: 0416 CutWidgetCommand(Form &form, const QWidgetList &list, Command *parent = 0); 0417 0418 virtual ~CutWidgetCommand(); 0419 0420 virtual int id() const override; 0421 0422 virtual void execute() override; 0423 0424 virtual void undo() override; 0425 0426 virtual void debug() const override; 0427 0428 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const CutWidgetCommand &c); 0429 protected: 0430 class Private; 0431 Private * const d2; 0432 }; 0433 0434 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0435 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const CutWidgetCommand &c); 0436 0437 //! Command that holds several PropertyCommand subcommands. 0438 /*! It appears as one to the user and in the command history. */ 0439 class KFORMDESIGNER_EXPORT PropertyCommandGroup : public Command 0440 { 0441 public: 0442 explicit PropertyCommandGroup(const QString &text, Command *parent = 0); 0443 0444 virtual ~PropertyCommandGroup(); 0445 0446 virtual int id() const override; 0447 0448 virtual void execute() override; 0449 0450 virtual void debug() const override; 0451 0452 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PropertyCommandGroup &c); 0453 protected: 0454 class Private; 0455 Private * const d; 0456 }; 0457 0458 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0459 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const PropertyCommandGroup &c); 0460 0461 //! Command is used when inline text is edited for a single widget. 0462 class KFORMDESIGNER_EXPORT InlineTextEditingCommand : public Command 0463 { 0464 public: 0465 /*! @a oldValue is the old property value for selected widget. 0466 This enables reverting the change. @a value is the new property value. */ 0467 InlineTextEditingCommand( 0468 Form& form, QWidget *widget, const QByteArray &editedWidgetClass, 0469 const QString &text, Command *parent = 0); 0470 0471 virtual ~InlineTextEditingCommand(); 0472 0473 virtual int id() const override; 0474 0475 virtual bool mergeWith(const KUndo2Command * command) override; 0476 0477 virtual void execute() override; 0478 0479 virtual void debug() const override; 0480 0481 virtual void undo() override; 0482 0483 Form* form() const; 0484 0485 QString text() const; 0486 0487 QString oldText() const; 0488 0489 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InlineTextEditingCommand &c); 0490 0491 protected: 0492 class Private; 0493 Private * const d; 0494 }; 0495 0496 //! qDebug() stream operator. Writes command group @a c to the debug output in a nicely formatted way. 0497 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InlineTextEditingCommand &c); 0498 0499 class KFORMDESIGNER_EXPORT InsertPageCommand : public Command 0500 { 0501 public: 0502 InsertPageCommand(Container *container, QWidget *widget); 0503 0504 virtual ~InsertPageCommand(); 0505 0506 virtual int id() const override; 0507 0508 virtual void execute() override; 0509 0510 void execute(const QString& pageWidgetName, const QString& pageName, int pageIndex); 0511 0512 virtual void debug() const override; 0513 0514 virtual void undo() override; 0515 0516 void undo(const QString& name); 0517 0518 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InsertPageCommand &c); 0519 0520 protected: 0521 class Private; 0522 Private * const d; 0523 }; 0524 0525 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0526 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const InsertPageCommand &c); 0527 0528 class KFORMDESIGNER_EXPORT RemovePageCommand : public Command 0529 { 0530 public: 0531 RemovePageCommand(Container *container, QWidget *widget); 0532 0533 virtual ~RemovePageCommand(); 0534 0535 virtual int id() const override; 0536 0537 virtual void execute() override; 0538 0539 virtual void debug() const override; 0540 0541 virtual void undo() override; 0542 0543 int pageIndex() const; 0544 0545 QString pageName() const; 0546 0547 friend KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const RemovePageCommand &c); 0548 0549 protected: 0550 class Private; 0551 Private * const d; 0552 }; 0553 0554 //! qDebug() stream operator. Writes command @a c to the debug output in a nicely formatted way. 0555 KFORMDESIGNER_EXPORT QDebug operator<<(QDebug dbg, const RemovePageCommand &c); 0556 0557 } 0558 0559 #endif