File indexing completed on 2024-12-08 04:00:42

0001 /***************************************************************************
0002                           imagemapeditor.h  -  description
0003                              -------------------
0004     begin                : Wed Apr 4 2001
0005     copyright            : (C) 2001 by Jan Schäfer
0006     email                : janschaefer@users.sourceforge.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef KIMAGEMAPEDITOR_H
0019 #define KIMAGEMAPEDITOR_H
0020 
0021 #include <QDockWidget>
0022 #include <QHash>
0023 #include <QImage>
0024 #include <QObject>
0025 #include <QPixmap>
0026 #include <QTextStream>
0027 #include <QUrl>
0028 
0029 #include <KConfig>
0030 #include <KParts/ReadWritePart>
0031 
0032 #include <list>
0033 
0034 #include "kimearea.h"
0035 #include "kimagemapeditorinterface.h"
0036 
0037 /**
0038   *@author Jan Schaefer
0039   */
0040 
0041 // #define WITH_TABWIDGET
0042 
0043 
0044 class QTreeWidget;
0045 class DrawZone;
0046 class QTreeWidgetItem;
0047 class KToggleAction;
0048 class KXmlGuiWindow;
0049 
0050 
0051 
0052 /**
0053  * Stores an area tag and all its attributes
0054  */
0055 typedef QHash<QString,QString> AreaTag;
0056 
0057 /**
0058  * Stores an image tag and all its attributes
0059  * the origcode attribute hold the original htmlcode
0060  * of this tag
0061  */
0062 typedef QHash<QString,QString> ImageTag;
0063 
0064 /**
0065  * Only a small class to give a list of AreaTags a name
0066  */
0067 class MapTag : public std::list<AreaTag> {
0068 public:
0069     MapTag();
0070     QString name;
0071     bool modified;
0072 };
0073 
0074 
0075 class HtmlElement {
0076 public:
0077     explicit HtmlElement(const QString & s) {
0078         htmlCode = s;
0079     };
0080     virtual ~HtmlElement() {}
0081     ;
0082     QString htmlCode;
0083 };
0084 
0085 class HtmlMapElement : public HtmlElement {
0086 public:
0087     explicit HtmlMapElement(const QString & s) : HtmlElement(s) {
0088       mapTag = nullptr;
0089     };
0090 
0091     ~HtmlMapElement() override {};
0092 
0093     MapTag* mapTag;
0094 };
0095 
0096 class HtmlImgElement : public HtmlElement {
0097 public:
0098     explicit HtmlImgElement(const QString & s) : HtmlElement(s) {
0099       imgTag = nullptr;
0100     };
0101     ~HtmlImgElement() override {}
0102     ;
0103     ImageTag* imgTag;
0104 };
0105 
0106 /**
0107  * Stores the hole HTML content in a List.
0108  */
0109 typedef QList<HtmlElement*> HtmlContent;
0110 
0111 
0112 class KSelectAction;
0113 class QAction;
0114 class KRecentFilesAction;
0115 ///class QListViewItem;
0116 class QUndoStack;
0117 class QTabWidget;
0118 class AreaListView;
0119 class ImagesListView;
0120 class MapsListView;
0121 class KAboutData;
0122 
0123 // needed by the statusbar
0124 #define STATUS_CURSOR 1000
0125 #define STATUS_SELECTION 1001
0126 
0127 class KImageMapEditor : public KParts::ReadWritePart, public KImageMapEditorInterface
0128 {
0129     Q_OBJECT
0130     Q_INTERFACES(KImageMapEditorInterface)
0131 
0132 public :
0133     enum ToolType { Selection,
0134             Rectangle,
0135             Circle,
0136             Polygon,
0137             Freehand,
0138             AddPoint,
0139             RemovePoint };
0140 
0141 # if QT_VERSION_MAJOR < 6
0142     KImageMapEditor(QWidget *parentWidget, QObject *parent,
0143                     const KPluginMetaData &metaData,
0144                     const QVariantList &args = QVariantList());
0145 #else
0146     KImageMapEditor(QWidget *parentWidget, QObject *parent,
0147                     const KPluginMetaData &metaData);
0148 #endif
0149     ~KImageMapEditor() override;
0150 
0151     /**
0152      * Return custom componentName for KXMLGUIClient, as for historical reasons the plugin id is not used
0153      */
0154     QString componentName() const override;
0155 
0156     static KConfig *config();
0157 
0158     /**
0159     * Makes sure, that the actions cut, copy, delete and
0160     * show properties
0161     * can only be executed if sth. is selected.
0162     **/
0163     void updateActionAccess();
0164 
0165     DrawZone* getDrawZone() {
0166         return drawZone;
0167     };
0168 
0169     void addAreaAndEdit(Area*);
0170     void addArea(Area*);
0171     AreaListIterator areaList() const;
0172     KImageMapEditor::ToolType currentToolType() const;
0173     void deleteSelected();
0174     void deleteArea( Area * area);
0175     void deleteAllAreas();
0176     void deselectAll();
0177     void deselect(Area* s);
0178     void deselectWithoutUpdate(Area*);
0179     QString getHTMLImageMap() const;
0180     Area* onArea(const QPoint & p) const;
0181     QPixmap makeListViewPix(Area &) ;
0182     QString mapName() const;
0183     void select(Area*);
0184     void selectWithoutUpdate(Area*);
0185     void select(QTreeWidgetItem*);
0186     AreaSelection* selected() const;
0187     void setPicture(const QImage & pix);
0188     int showTagEditor(Area *);
0189     QUndoStack *commandHistory() const;
0190 
0191     // Only refreshes the listView
0192     void updateSelection() const;
0193 
0194     void readConfig();
0195     void writeConfig();
0196 
0197     void readProperties(const KConfigGroup &) override;
0198     void saveProperties(KConfigGroup &) override;
0199     using KParts::ReadWritePart::closeUrl;
0200     bool closeUrl() override;
0201     bool queryClose() override;
0202     void setReadWrite(bool) override;
0203     QString getHtmlCode();
0204 
0205     /**
0206      * Reimplemented to disable and enable Save action
0207      */
0208     void setModified(bool) override;
0209 
0210     /**
0211      * Opens the given file.
0212      * If it's an HTML file openURL is called
0213      * If it's an Image, the image is added to the image list
0214      */
0215     void openFile(const QUrl &) override;
0216 
0217     /**
0218      * Opens the last URL the user worked with.
0219      * Sets also, the last map and the last image
0220      */
0221     void openLastURL(const KConfigGroup &) override;
0222 
0223     void readConfig(const KConfigGroup &);
0224     void writeConfig(KConfigGroup &);
0225 
0226 
0227 protected:
0228     void init();
0229     bool openHTMLFile(const QUrl &);
0230     void saveImageMap(const QUrl &);
0231 
0232     /**
0233      * Returns a language dependent background picture, with
0234      * the text : Drop an image or html file
0235      */
0236     QImage getBackgroundImage();
0237 
0238 
0239     /**
0240      * Saves information to restore the last working state
0241      */
0242     void saveLastURL(KConfigGroup&);
0243 
0244 
0245 private:
0246     // Stores the hole html file in a List
0247     // The entries are either a MapTag an ImageTag or a QString
0248     HtmlContent _htmlContent;
0249 
0250     // the url of the working image;
0251     QUrl _imageUrl;
0252     QString _mapName;
0253     QImage _backgroundImage;
0254 
0255     bool backupFileCreated;
0256 
0257     KImageMapEditor::ToolType _currentToolType;
0258     AreaList *areas;
0259     AreaSelection *currentSelected;
0260     AreaSelection *copyArea;
0261     Area *defaultArea;
0262     DrawZone* drawZone;
0263     QTabWidget* tabWidget;
0264     AreaListView *areaListView;
0265     ImagesListView* imagesListView;
0266     MapsListView* mapsListView;
0267     HtmlMapElement* currentMapElement;
0268 
0269     //
0270     // Actions
0271     //
0272     KSelectAction* zoomAction;
0273     QAction *arrowAction;
0274     QAction *circleAction;
0275     QAction *rectangleAction;
0276     QAction *polygonAction;
0277     QAction *freehandAction;
0278     QAction *addPointAction;
0279     QAction *removePointAction;
0280 
0281     QAction *cutAction;
0282     QAction *deleteAction;
0283     QAction *copyAction;
0284     QAction *pasteAction;
0285     QAction *zoomInAction;
0286     QAction *zoomOutAction;
0287 
0288     QAction *mapNewAction;
0289     QAction *mapDeleteAction;
0290     QAction *mapNameAction;
0291     QAction *mapDefaultAreaAction;
0292 
0293     QAction *imageAddAction;
0294     QAction *imageRemoveAction;
0295     QAction *imageUsemapAction;
0296 
0297     KToggleAction *highlightAreasAction;
0298     KToggleAction *showAltAction;
0299 
0300     QAction *areaPropertiesAction;
0301 
0302     QAction *moveLeftAction;
0303     QAction *moveRightAction;
0304     QAction *moveUpAction;
0305     QAction *moveDownAction;
0306 
0307     QAction *increaseWidthAction;
0308     QAction *decreaseWidthAction;
0309     QAction *increaseHeightAction;
0310     QAction *decreaseHeightAction;
0311 
0312     QAction *toFrontAction;
0313     QAction *toBackAction;
0314     QAction *forwardOneAction;
0315     QAction *backOneAction;
0316 
0317     KToggleAction* configureShowAreaListAction;
0318     KToggleAction* configureShowMapListAction;
0319     KToggleAction* configureShowImageListAction;
0320 
0321 
0322     KRecentFilesAction* recentFilesAction;
0323 
0324     KXmlGuiWindow *mainWindow;
0325     QDockWidget* areaDock;
0326     QDockWidget* mapsDock;
0327     QDockWidget* imagesDock;
0328 
0329     QUndoStack *_commandHistory;
0330     int maxAreaPreviewHeight;
0331 
0332     QString cursorStatusText;
0333     QString selectionStatusText;
0334 
0335     void setupActions();
0336     void setupStatusBar();
0337     void updateStatusBar();
0338     /* refreshes all Areas, only used by preferences dialog
0339      * updates only the preview pictures*/
0340     void updateAllAreas();
0341     void updateUpDownBtn();
0342 
0343     QHash<QString,QString> getTagAttributes(QTextStream & s,QString &);
0344 
0345     void setMap(HtmlMapElement*);
0346     void setMap(MapTag*);
0347     void addMap(const QString &);
0348 
0349     // Returns the entire html file as a String
0350     HtmlElement* findHtmlElement(const QString &);
0351     HtmlImgElement* findHtmlImgElement(ImageTag*);
0352     HtmlMapElement* findHtmlMapElement(const QString &);
0353     void deleteAllMaps();
0354     void addImage(const QUrl &);
0355     void setImageActionsEnabled(bool);
0356     void setMapActionsEnabled(bool);
0357 
0358     void saveAreasToMapTag(MapTag*);
0359     void showPopupMenu(const QPoint &, const QString &);
0360     void drawToCenter(QPainter* p, const QString & str, int y, int width);
0361 
0362 public slots:
0363     bool openURL(const QUrl & url) override;
0364     void slotChangeStatusCoords(int x,int y);
0365     void slotUpdateSelectionCoords();
0366     void slotUpdateSelectionCoords( const QRect &);
0367     void slotAreaChanged(Area *);
0368     void slotShowMainPopupMenu(const QPoint &);
0369     void slotShowMapPopupMenu(const QPoint &);
0370     void slotShowImagePopupMenu(const QPoint &);
0371     void slotConfigChanged();
0372     void setPicture(const QUrl & url);
0373     void setMap(const QString &);
0374     void setMapName(const QString & s);
0375 
0376 
0377 protected slots:
0378     // overridden from KReadWritePart
0379     bool openFile() override;
0380 
0381     bool saveFile() override {
0382         saveImageMap( url() );
0383 //        setModified(false);
0384         return true;
0385     }
0386 
0387     void fileOpen();
0388     void fileSaveAs();
0389     void fileSave();
0390     void fileClose();
0391 
0392     void slotShowPopupMenu(const QPoint &);
0393     void slotShowPreferences();
0394     void slotHighlightAreas(bool b);
0395     void slotShowAltTag(bool b);
0396     void slotSelectionChanged();
0397 
0398     int showTagEditor(QTreeWidgetItem *item);
0399     int showTagEditor();
0400 
0401     void slotZoom();
0402     void slotZoomIn();
0403     void slotZoomOut();
0404 
0405     void slotCut();
0406     void slotCopy();
0407     void slotPaste();
0408     void slotDelete();
0409 
0410     void slotDrawArrow();
0411     void slotDrawCircle();
0412     void slotDrawRectangle();
0413     void slotDrawPolygon();
0414     void slotDrawFreehand();
0415     void slotDrawAddPoint();
0416     void slotDrawRemovePoint();
0417 
0418     void mapDefaultArea();
0419     void mapNew();
0420     void mapDelete();
0421     void mapEditName();
0422     void mapShowHTML();
0423     void mapPreview();
0424 
0425     void slotBackOne();
0426     void slotForwardOne();
0427     void slotToBack();
0428     void slotToFront();
0429 
0430     void slotMoveUp();
0431     void slotMoveDown();
0432     void slotMoveLeft();
0433     void slotMoveRight();
0434 
0435     void slotIncreaseHeight();
0436     void slotDecreaseHeight();
0437     void slotIncreaseWidth();
0438     void slotDecreaseWidth();
0439 
0440     void slotCancelDrawing();
0441 
0442     //  void slotPreferences();
0443     void imageAdd();
0444     void imageRemove();
0445     void imageUsemap();
0446 
0447 };
0448 
0449 
0450 inline KImageMapEditor::ToolType KImageMapEditor::currentToolType() const {
0451     return _currentToolType;
0452 }
0453 
0454 inline QString KImageMapEditor::mapName() const {
0455     return _mapName;
0456 }
0457 
0458 inline QUndoStack* KImageMapEditor::commandHistory() const {
0459     return _commandHistory;
0460 }
0461 
0462 #endif // IMAGEMAPEDITOR_H