File indexing completed on 2024-12-22 04:17:50

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                   netterfield@astro.utoronto.ca                         *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #ifndef VIEWITEM_H
0014 #define VIEWITEM_H
0015 
0016 #include <QObject>
0017 #include <QGraphicsRectItem>
0018 #include <QGraphicsSceneContextMenuEvent>
0019 #include <QXmlStreamWriter>
0020 #include <QHash>
0021 #include <QAction>
0022 #include <QMimeData>
0023 
0024 #include "namedobject.h"
0025 #include "kst_export.h"
0026 #include "viewcommand.h"
0027 #include "view.h"
0028 #include "curveplacement.h"
0029 //#include "sharedptr.h"
0030 #include "application.h"
0031 #include "tabwidget.h"
0032 
0033 namespace Kst {
0034 
0035 class DialogPage;
0036 class ViewGridLayout;
0037 class ViewItem;
0038 class ViewItemDialog;
0039 class ScriptInterface;
0040 
0041 typedef QList<ViewItem *> ViewItemList;
0042 
0043 class ViewItem : public QObject, public NamedObject, public QGraphicsRectItem
0044 {
0045   Q_OBJECT
0046   public:
0047     friend class DialogLauncherSI;
0048     friend class ViewItemSI;
0049     enum GripMode {
0050       Move    = 1,
0051       Resize  = 2,
0052       Scale   = 4,
0053       Rotate  = 8
0054     };
0055     Q_DECLARE_FLAGS(GripModes, GripMode)
0056 
0057     enum ActiveGrip {
0058       NoGrip          = 1,
0059       TopLeftGrip     = 2,
0060       TopRightGrip    = 4,
0061       BottomRightGrip = 8,
0062       BottomLeftGrip  = 16,
0063       TopMidGrip      = 32,
0064       RightMidGrip    = 64,
0065       BottomMidGrip   = 128,
0066       LeftMidGrip     = 256
0067     };
0068     Q_DECLARE_FLAGS(ActiveGrips, ActiveGrip)
0069 
0070     enum CreationState {
0071       None,
0072       InProgress,
0073       Completed
0074     };
0075 
0076     explicit ViewItem(View *parent);
0077     virtual ~ViewItem();
0078 
0079     virtual void save(QXmlStreamWriter &xml);
0080     bool parse(QXmlStreamReader &xml, bool &validChildTag);
0081     enum { Type = UserType + 1 };
0082     int type() const { return Type; }
0083 
0084     void setTypeName(const QString& name) { _typeName = name; }
0085     const QString typeName() const { return _typeName; }
0086 
0087     // for dialog defaults
0088     virtual const QString defaultsGroupName() const = 0;
0089 
0090     // for view item dialogs
0091     virtual bool hasStroke() const {return false;}
0092     virtual bool hasBrush() const {return false;}
0093     virtual bool hasFont() const {return false;}
0094 
0095     virtual void setFont(const QFont &f, const QColor &c) {return;}
0096 
0097     View* view() const;
0098     void setView(View*);
0099 
0100     ViewItem *parentViewItem() const;
0101     void setParentViewItem(ViewItem *parent);
0102 
0103     virtual void updateRelativeSize(bool force_data = false);
0104     virtual void updateDataRelativeRect(bool force = false);
0105 
0106     void moveTo(const QPointF& pos);
0107     
0108     qreal relativeHeight() const { return _parentRelativeHeight; }
0109     void setRelativeHeight(const qreal height) { _parentRelativeHeight = height; }
0110     qreal relativeWidth() const { return _parentRelativeWidth; }
0111     void setRelativeWidth(const qreal width) { _parentRelativeWidth = width; }
0112 
0113     QPointF relativeCenter() const { return _parentRelativeCenter; }
0114     void setRelativeCenter(const QPointF center) { _parentRelativeCenter = center; }
0115     QPointF relativePosition() const { return _parentRelativePosition; }
0116     void setRelativePosition(const QPointF pos) { _parentRelativePosition = pos; }
0117     void setRelativeLeft(const QPointF &pos) { _parentRelativeLeft = pos;}
0118     QPointF relativeRight() const {return _parentRelativeRight;}
0119     void setRelativeRight(const QPointF &pos) { _parentRelativeRight = pos;}
0120 
0121     QRectF dataRelativeRect() const { return _dataRelativeRect;}
0122     void setDataRelativeRect(QRectF r) { _dataRelativeRect = r;}
0123     
0124     void setItemPos(qreal x, qreal y);
0125     void setItemSize(qreal w, qreal h = -10.0);
0126 
0127     qreal rotationAngle() const;
0128     qreal rotationAngleRadians() const;
0129 
0130     GripMode gripMode() const;
0131     void setGripMode(GripMode mode);
0132 
0133     GripModes allowedGripModes() const;
0134     void setAllowedGripModes(GripModes modes);
0135     bool isAllowed(GripMode mode) const;
0136 
0137     bool fixedSize() const { return _fixedSize; }
0138     void setFixedSize(bool fixedSize) { _fixedSize = fixedSize; }
0139 
0140     bool lockAspectRatio() const { return _lockAspectRatio; }
0141     void setLockAspectRatio(bool lockAspectRatio) { _lockAspectRatio = lockAspectRatio; }
0142 
0143     bool lockAspectRatioFixed() const { return _lockAspectRatioFixed; }
0144     void setLockAspectRatioFixed(bool enable) { _lockAspectRatioFixed = enable; }
0145 
0146     bool hasStaticGeometry() const { return _hasStaticGeometry; }
0147     void setHasStaticGeometry(bool hasStaticGeometry ) { _hasStaticGeometry = hasStaticGeometry; }
0148 
0149     bool lockParent() const { return _lockParent; }
0150     void setLockParent(bool lockParent ) { _lockParent = lockParent; }
0151 
0152     bool skipNextParentCheck() const { return _skipNextParentCheck; }
0153     void setSkipNextParentCheck(bool skipNextParentCheck) { _skipNextParentCheck = skipNextParentCheck; }
0154 
0155     bool allowsLayout() const { return _allowsLayout; }
0156     void setAllowsLayout(bool allowsLayout ) { _allowsLayout = allowsLayout; }
0157 
0158     bool isHighlighted() const { return _highlighted; }
0159     void setHighlighted(bool highlighted ) { _highlighted = highlighted; }
0160 
0161     bool lockPosToData() const {return _lockPosToData; }
0162 
0163     //NOTE This should be used in place of QGraphicsRectItem::setRect()...
0164     QRectF viewRect() const;
0165     void setViewRect(const QRectF &viewRect, bool automaticChange = false);
0166     void setViewRect(qreal x, qreal y, qreal width, qreal height, bool automaticChange = false);
0167 
0168     qreal width() const { return viewRect().normalized().width(); }
0169     qreal height() const { return viewRect().normalized().height(); }
0170 
0171     ActiveGrip activeGrip() const;
0172     void setActiveGrip(ActiveGrip grip);
0173 
0174     ActiveGrips allowedGrips() const;
0175     void setAllowedGrips(ActiveGrips grips);
0176     bool isAllowed(ActiveGrip grip) const;
0177 
0178     virtual QSizeF sizeOfGrip() const;
0179     virtual QPainterPath grips() const;
0180 
0181     virtual QRectF selectBoundingRect() const;
0182     virtual QRectF gripBoundingRect() const;
0183 
0184     virtual QRectF boundingRect() const;
0185     virtual QPainterPath shape() const;
0186     virtual QPainterPath itemShape() const { return QGraphicsRectItem::shape(); }
0187     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
0188     virtual void paint(QPainter *painter);
0189 
0190     virtual void paintChildItems(QPainter &painter);
0191 
0192     virtual QList<DialogPage*> dialogPages() const { return QList<DialogPage*>(); }
0193 
0194     virtual QPointF centerOfRotation() const { return rect().center(); }
0195 
0196     bool isHovering() const { return _hovering; }
0197 
0198     bool acceptsChildItems() const { return _acceptsChildItems; }
0199     void setAcceptsChildItems(bool acceptsChildItems) { _acceptsChildItems = acceptsChildItems; }
0200 
0201     QSizeF layoutMargins() const { return _layoutMargins; }
0202     void setLayoutMargins(const QSizeF margins) { _layoutMargins = margins; }
0203 
0204     QSizeF layoutSpacing() const { return _layoutSpacing; }
0205     void setLayoutSpacing(const QSizeF spacing) { _layoutSpacing = spacing; }
0206 
0207     //This is a workaround for context menu bug in Qt4.3 graphicsview
0208     bool acceptsContextMenuEvents() const
0209     { return _acceptsContextMenuEvents; }
0210     void setAcceptsContextMenuEvents(bool acceptsContextMenuEvents)
0211     { _acceptsContextMenuEvents = acceptsContextMenuEvents; }
0212 
0213     virtual bool tryShortcut(const QString &keySequence);
0214     virtual QPainterPath checkBox() const;
0215     virtual QPainterPath tiedZoomCheck() const;
0216     virtual QSizeF tiedZoomSize() const { return QSizeF(checkBox().controlPointRect().size() * 1.5); }
0217 
0218     virtual bool isTiedZoom() const { return (_isXTiedZoom || _isYTiedZoom); }
0219     virtual bool isXTiedZoom() const { return _isXTiedZoom; }
0220     virtual bool isYTiedZoom() const { return _isYTiedZoom; }
0221     virtual void setTiedZoom(bool tiedXZoom, bool tiedYZoom, bool checkAllTied = true);
0222 
0223     virtual bool supportsTiedZoom() const { return _supportsTiedZoom; }
0224     virtual void setSupportsTiedZoom(const bool supports);    
0225 
0226 
0227     View::ZoomOnlyMode zoomOnlyMode() const { return _zoomOnlyMode; }
0228     void setZoomOnly(View::ZoomOnlyMode val) { _zoomOnlyMode = val; }
0229 
0230     CreationState creationState() const { return _creationState; }
0231 
0232     virtual void setItemPen(const QPen & pen) { storePen(pen); }
0233     virtual void setItemBrush(const QBrush & brush) { setBrush(brush); }
0234 
0235     template<class T> static T* retrieveItem(const QString &name);
0236 
0237     template<class T> static QList<T *> getItems(bool include_hidden=false);
0238 
0239    // TODO: Remove, needed only for a Qt 4.3 bug workaround
0240     bool doSceneEvent(QGraphicsSceneContextMenuEvent *event) {
0241       return sceneEvent(event);
0242     }
0243 
0244     virtual bool isMaximized();
0245     QPointF dropHotSpot;
0246 
0247     void normalizePosition();
0248 
0249     virtual void applyDialogDefaultsFill(bool default_no_fill = false);
0250     virtual void applyDialogDefaultsStroke(bool default_no_pen = false);
0251     void applyDialogDefaultsLockPosToData();
0252 
0253     virtual void applyDataLockedDimensions();
0254     virtual bool dataPosLockable() const;
0255 
0256     QRectF parentRect() const;
0257 
0258     void storePen(const QPen &pen) {_storedPen = pen; setPen(pen);}
0259     QPen storedPen() const { return _storedPen;}
0260 
0261     virtual ScriptInterface *createScriptInterface();
0262 
0263     ScriptInterface *scriptInterface();
0264 
0265     virtual bool updateViewItemParent(bool force_toplevel = false);
0266 
0267   Q_SIGNALS:
0268     void geometryChanged();
0269     void creationComplete();
0270     void relativeSizeUpdated();
0271 
0272   /*FIXME these should be made private for only undo commands to access*/
0273   public Q_SLOTS:
0274     virtual void edit();
0275     virtual void raise();
0276     virtual void lower();
0277     virtual void createAutoLayout();
0278     virtual void createProtectedLayout();
0279     virtual void createCustomLayout(int columns = 0);
0280     virtual void createOneColLayout() {createCustomLayout(1);}
0281     virtual void createTwoColLayout() {createCustomLayout(2);}
0282     virtual void createThreeColLayout() {createCustomLayout(3);}
0283     virtual void sharePlots(QPainter *painter, bool creation);
0284     virtual void remove();
0285     void resizeTopLeft(const QPointF &offset);
0286     void resizeTopRight(const QPointF &offset);
0287     void resizeBottomLeft(const QPointF &offset);
0288     void resizeBottomRight(const QPointF &offset);
0289     void resizeTop(qreal offset);
0290     void resizeBottom(qreal offset);
0291     void resizeLeft(qreal offset);
0292     void resizeRight(qreal offset);
0293     void setTopLeft(const QPointF &point);
0294     void setTopRight(const QPointF &point);
0295     void setBottomLeft(const QPointF &point);
0296     void setBottomRight(const QPointF &point);
0297     void setTop(qreal y);
0298     void setBottom(qreal y);
0299     void setLeft(qreal x);
0300     void setRight(qreal x);
0301     void setLockPosToData(bool lockPosToData);
0302     virtual bool customDimensionsTab() {return false;}
0303 
0304     virtual void clearEditDialogPtr() {_editDialog = 0;}
0305   protected:
0306     virtual QPainterPath topLeftGrip() const;
0307     virtual QPainterPath topRightGrip() const;
0308     virtual QPainterPath bottomRightGrip() const;
0309     virtual QPainterPath bottomLeftGrip() const;
0310     virtual QPainterPath topMidGrip() const;
0311     virtual QPainterPath rightMidGrip() const;
0312     virtual QPainterPath bottomMidGrip() const;
0313     virtual QPainterPath leftMidGrip() const;
0314     QTransform selectTransform() const;
0315     bool transformToRect(const QRectF &from, const QRectF &to);
0316     bool transformToRect(const QPolygonF &from, const QPolygonF &to);
0317     void rotateTowards(const QPointF &corner, const QPointF &point);
0318     QPointF lockOffset(const QPointF &offset, qreal ratio, bool oddCorner) const;
0319     GripMode nextGripMode(GripMode currentMode) const;
0320     void addTitle(QMenu *menu) const;
0321     void registerShortcut(QAction *action);
0322     void reRegisterShortcut();
0323 
0324     QString descriptionTip() const;
0325 
0326   protected Q_SLOTS:
0327     virtual void creationPolygonChanged(View::CreationEvent event);
0328     void creationPolygonChangedFixedAspect(View::CreationEvent even, qreal aspect);
0329 
0330   protected:
0331     virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
0332     virtual void addToMenuForContextEvent(QMenu &menu);
0333     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
0334     virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
0335     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
0336     virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
0337     virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
0338     virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
0339     virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
0340     virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
0341 
0342 
0343     QAction *_editAction;
0344     QAction *_deleteAction;
0345     QAction *_raiseAction;
0346     QAction *_lowerAction;
0347     QAction *_autoLayoutAction;
0348     QAction *_protectedLayoutAction;
0349     QAction *_customLayoutAction;
0350     QAction *_oneColumnLayoutAction;
0351     QAction *_twoColumnLayoutAction;
0352     QAction *_threeColumnLayoutAction;
0353     QAction *_lockPosToDataAction;
0354 
0355     bool _isXTiedZoom;
0356     bool _isYTiedZoom;
0357     bool _plotMaximized;
0358 
0359   private Q_SLOTS:
0360     void viewMouseModeChanged(View::MouseMode oldMode);
0361     void updateView();
0362 
0363   protected:
0364   public:
0365     virtual void updateChildGeometry(const QRectF &oldParentRect, const QRectF &newParentRect);
0366   protected:
0367     virtual QString _automaticDescriptiveName() const;
0368     virtual void _initializeShortName();
0369     QPointF dragStartPosition;
0370     void startDragging(QWidget *widget, const QPointF& hotspot);
0371     ActiveGrip _activeGrip;
0372     QRectF _dataRelativeRect;
0373     QPointF _originalPosition;
0374     QPointF _parentRelativeCenter;
0375     QPointF _parentRelativePosition;
0376     QPointF _parentRelativeLeft;
0377     QPointF _parentRelativeRight;
0378     qreal _parentRelativeHeight, _parentRelativeWidth;
0379     const double dpi() { return _dpi;}
0380 
0381   private:
0382     GripMode _gripMode;
0383     GripModes _allowedGripModes;
0384     CreationState _creationState;
0385     QString _typeName;
0386     View::ZoomOnlyMode _zoomOnlyMode;
0387     bool _supportsTiedZoom;    
0388     bool _fixedSize;
0389     bool _lockAspectRatio;
0390     bool _lockAspectRatioFixed;
0391     bool _hasStaticGeometry;
0392     bool _lockParent;
0393     bool _skipNextParentCheck;
0394     bool _allowsLayout;
0395     bool _hovering;
0396     bool _acceptsChildItems;
0397     bool _acceptsContextMenuEvents;
0398     bool _updatingLayout;
0399     bool _highlighted;
0400     QRectF _originalRect;
0401     QTransform _originalTransform;
0402     QLineF _normalLine;
0403     QLineF _rotationLine;
0404     ActiveGrips _allowedGrips;
0405     QTransform _rotationTransform;
0406     QHash<QString, QAction*> _shortcutMap;
0407 
0408     bool _lockPosToData;
0409 
0410     QSizeF _layoutMargins, _layoutSpacing;    
0411 
0412     // use view() / setView(View*)
0413     QObject* parent() const;
0414     void setParent(QObject*);
0415 
0416     // use setParentViewItem(ViewItem*)
0417     void setParentItem(QGraphicsItem*);
0418 
0419     ViewItemDialog *_editDialog;
0420     QPen _storedPen;
0421     ScriptInterface *_interface;
0422     double _dpi;
0423 };
0424 
0425 Q_DECLARE_OPERATORS_FOR_FLAGS(ViewItem::GripModes)
0426 Q_DECLARE_OPERATORS_FOR_FLAGS(ViewItem::ActiveGrips)
0427 
0428 #ifndef QT_NO_DEBUG_STREAM
0429 QDebug operator<<(QDebug, ViewItem*);
0430 #endif
0431 
0432 class ViewItemCommand : public QUndoCommand
0433 {
0434   public:
0435     ViewItemCommand(ViewItem *item, const QString &text, bool addToStack = true, QUndoCommand *parent = 0);
0436     virtual ~ViewItemCommand();
0437 
0438   protected:
0439     QPointer<ViewItem> _item;
0440 };
0441 
0442 class CreateCommand : public QObject, public ViewCommand
0443 {
0444   Q_OBJECT
0445   public:
0446     explicit CreateCommand(const QString &text, QUndoCommand *parent = 0);
0447     CreateCommand(View *view, const QString &text, QUndoCommand *parent = 0);
0448     virtual ~CreateCommand();
0449 
0450     virtual void undo();
0451     virtual void redo();
0452     virtual void createItem();
0453 
0454     ViewItem *item() const { return _item; }
0455 
0456   public Q_SLOTS:
0457     virtual void creationComplete();
0458 
0459   protected:
0460     QPointer<ViewItem> _item;
0461 };
0462 
0463 class LayoutCommand : public ViewItemCommand
0464 {
0465   public:
0466     explicit LayoutCommand(ViewItem *item)
0467         : ViewItemCommand(item, QObject::tr("Create layout"), false) {}
0468 
0469     virtual ~LayoutCommand() {}
0470 
0471     virtual void undo();
0472     virtual void redo();
0473     void createLayout(bool preserve = true, int columns = 0);
0474   private:
0475     QPointer<ViewGridLayout> _layout;
0476 };
0477 
0478 class AppendLayoutCommand : public ViewItemCommand
0479 {
0480   public:
0481     explicit AppendLayoutCommand(ViewItem *item)
0482         : ViewItemCommand(item, QObject::tr("Append Item to Layout"), false) {}
0483 
0484     virtual ~AppendLayoutCommand() {}
0485 
0486     virtual void undo();
0487     virtual void redo();
0488     void appendLayout(CurvePlacement::Layout layout, ViewItem* item, int columns = 0);
0489 
0490   private:
0491     QPointer<ViewGridLayout> _layout;
0492 };
0493 
0494 class MoveCommand : public ViewItemCommand
0495 {
0496   public:
0497     MoveCommand(ViewItem *item, const QPointF &originalPos, const QPointF &newPos)
0498         : ViewItemCommand(item, QObject::tr("Move")),
0499           _originalPos(originalPos),
0500           _newPos(newPos) {}
0501 
0502     virtual ~MoveCommand() {}
0503 
0504     virtual void undo();
0505     virtual void redo();
0506 
0507   private:
0508     QPointF _originalPos;
0509     QPointF _newPos;
0510 };
0511 
0512 class ResizeCommand : public ViewItemCommand
0513 {
0514   public:
0515     ResizeCommand(ViewItem *item, const QRectF &originalRect, const QRectF &newRect)
0516         : ViewItemCommand(item, QObject::tr("Resize")),
0517           _originalRect(originalRect),
0518           _newRect(newRect) {}
0519 
0520     virtual ~ResizeCommand() {}
0521 
0522     virtual void undo();
0523     virtual void redo();
0524 
0525   private:
0526     QRectF _originalRect;
0527     QRectF _newRect;
0528 };
0529 
0530 class RemoveCommand : public ViewItemCommand
0531 {
0532   public:
0533     explicit RemoveCommand(ViewItem *item)
0534         : ViewItemCommand(item, QObject::tr("Remove")) {}
0535 
0536     virtual ~RemoveCommand() {}
0537 
0538     virtual void undo();
0539     virtual void redo();
0540 };
0541 
0542 class RaiseCommand : public ViewItemCommand
0543 {
0544   public:
0545     explicit RaiseCommand(ViewItem *item)
0546         : ViewItemCommand(item, QObject::tr("Raise")) {}
0547 
0548     virtual ~RaiseCommand() {}
0549 
0550     virtual void undo();
0551     virtual void redo();
0552 };
0553 
0554 class LowerCommand : public ViewItemCommand
0555 {
0556   public:
0557     explicit LowerCommand(ViewItem *item)
0558         : ViewItemCommand(item, QObject::tr("Lower")) {}
0559 
0560     virtual ~LowerCommand() {}
0561 
0562     virtual void undo();
0563     virtual void redo();
0564 };
0565 
0566 class TransformCommand : public ViewItemCommand
0567 {
0568   public:
0569     TransformCommand(ViewItem *item, const QTransform &originalTransform,
0570                   const QTransform &newTransform, const QString &text)
0571         : ViewItemCommand(item, text),
0572           _originalTransform(originalTransform), _newTransform(newTransform) {}
0573 
0574     virtual ~TransformCommand() {}
0575 
0576     virtual void undo();
0577     virtual void redo();
0578 
0579   private:
0580     QTransform _originalTransform;
0581     QTransform _newTransform;
0582 };
0583 
0584 class ScaleCommand : public TransformCommand
0585 {
0586   public:
0587     ScaleCommand(ViewItem *item, const QTransform &originalTransform, const QTransform &newTransform)
0588         : TransformCommand(item, originalTransform, newTransform, QObject::tr("Scale")) {}
0589 
0590     virtual ~ScaleCommand() {}
0591 };
0592 
0593 class RotateCommand : public TransformCommand
0594 {
0595   public:
0596     RotateCommand(ViewItem *item, const QTransform &originalTransform, const QTransform &newTransform)
0597         : TransformCommand(item, originalTransform, newTransform, QObject::tr("Rotate")) {}
0598 
0599     virtual ~RotateCommand() {}
0600 };
0601 
0602 
0603 template<class T>
0604 QList<T *> ViewItem::getItems(bool include_hidden) {
0605   QList<T *> tItems;
0606   ViewItem *viewItem;
0607   T* tItem;
0608 
0609   QList<View*> views = kstApp->mainWindow()->tabWidget()->views();
0610 
0611   for (int i_view = 0; i_view<views.count(); i_view++) {
0612     QList<QGraphicsItem*> items = views.at(i_view)->scene()->items();
0613     for (int i_item = 0; i_item<items.count(); i_item++) {
0614       viewItem = dynamic_cast<ViewItem *>(items[i_item]);
0615       tItem = dynamic_cast<T*>(viewItem);
0616       if ((tItem) && (include_hidden || viewItem->isVisible())) {
0617         tItems.append(tItem);
0618       }
0619     }
0620   }
0621   return tItems;
0622 }
0623 
0624 template<class T>
0625 T* ViewItem::retrieveItem(const QString &name) {
0626   QList<T *> tItems = getItems<T>();
0627 
0628   int match = -1;
0629 
0630   if (name.isEmpty()) {
0631     return NULL;
0632   }
0633 
0634   QString shortName;
0635   QRegExp rx("(\\(|^)([A-Z]\\d+)(\\)$|$)");
0636   rx.indexIn(name);
0637   shortName = rx.cap(2);
0638 
0639   // 1) search for short names
0640   int size = tItems.size();
0641   for (int i = 0; i < size; ++i) {
0642     if (tItems.at(i)->shortName()==shortName)
0643       return tItems.at(i);
0644   }
0645   // 3) search for descriptive names: must be unique
0646   for (int i = 0; i < size; ++i) {
0647     if (tItems.at(i)->descriptiveName() == name) {
0648       if (match != -1)
0649         return NULL; // not unique, so... no match
0650       match = i;
0651     }
0652   }
0653 
0654   if (match >-1)
0655     return tItems.at(match);
0656 
0657   return NULL;
0658 }
0659 
0660 
0661 class MimeDataViewItem : public QMimeData
0662 {
0663   Q_OBJECT
0664 public:
0665   MimeDataViewItem();
0666 
0667   ViewItem* item;
0668   QPointF hotSpot;
0669 
0670   static const MimeDataViewItem* downcast(const QMimeData*);
0671 };
0672 
0673 }
0674 
0675 #endif
0676 
0677 // vim: ts=2 sw=2 et