File indexing completed on 2023-05-30 11:08:59

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     KImageMapEditor(QWidget *parentWidget, QObject *parent,
0142                     const KPluginMetaData &metaData,
0143                     const QVariantList &args = QVariantList());
0144     ~KImageMapEditor() override;
0145 
0146     /**
0147      * Return custom componentName for KXMLGUIClient, as for historical reasons the plugin id is not used
0148      */
0149     QString componentName() const override;
0150 
0151     static KConfig *config();
0152 
0153     /**
0154     * Makes sure, that the actions cut, copy, delete and
0155     * show properties
0156     * can only be executed if sth. is selected.
0157     **/
0158     void updateActionAccess();
0159 
0160     DrawZone* getDrawZone() {
0161         return drawZone;
0162     };
0163 
0164     void addAreaAndEdit(Area*);
0165     void addArea(Area*);
0166     AreaListIterator areaList() const;
0167     KImageMapEditor::ToolType currentToolType() const;
0168     void deleteSelected();
0169     void deleteArea( Area * area);
0170     void deleteAllAreas();
0171     void deselectAll();
0172     void deselect(Area* s);
0173     void deselectWithoutUpdate(Area*);
0174     QString getHTMLImageMap() const;
0175     Area* onArea(const QPoint & p) const;
0176     QPixmap makeListViewPix(Area &) ;
0177     QString mapName() const;
0178     void select(Area*);
0179     void selectWithoutUpdate(Area*);
0180     void select(QTreeWidgetItem*);
0181     AreaSelection* selected() const;
0182     void setPicture(const QImage & pix);
0183     int showTagEditor(Area *);
0184     QUndoStack *commandHistory() const;
0185 
0186     // Only refreshes the listView
0187     void updateSelection() const;
0188 
0189     void readConfig();
0190     void writeConfig();
0191 
0192     void readProperties(const KConfigGroup &) override;
0193     void saveProperties(KConfigGroup &) override;
0194     using KParts::ReadWritePart::closeUrl;
0195     bool closeUrl() override;
0196     bool queryClose() override;
0197     void setReadWrite(bool) override;
0198     QString getHtmlCode();
0199 
0200     /**
0201      * Reimplemented to disable and enable Save action
0202      */
0203     void setModified(bool) override;
0204 
0205     /**
0206      * Opens the given file.
0207      * If it's an HTML file openURL is called
0208      * If it's an Image, the image is added to the image list
0209      */
0210     void openFile(const QUrl &) override;
0211 
0212     /**
0213      * Opens the last URL the user worked with.
0214      * Sets also, the last map and the last image
0215      */
0216     void openLastURL(const KConfigGroup &) override;
0217 
0218     void readConfig(const KConfigGroup &);
0219     void writeConfig(KConfigGroup &);
0220 
0221 
0222 protected:
0223     void init();
0224     bool openHTMLFile(const QUrl &);
0225     void saveImageMap(const QUrl &);
0226 
0227     /**
0228      * Returns a language dependent background picture, with
0229      * the text : Drop an image or html file
0230      */
0231     QImage getBackgroundImage();
0232 
0233 
0234     /**
0235      * Saves information to restore the last working state
0236      */
0237     void saveLastURL(KConfigGroup&);
0238 
0239 
0240 private:
0241     // Stores the hole html file in a List
0242     // The entries are either a MapTag an ImageTag or a QString
0243     HtmlContent _htmlContent;
0244 
0245     // the url of the working image;
0246     QUrl _imageUrl;
0247     QString _mapName;
0248     QImage _backgroundImage;
0249 
0250     bool backupFileCreated;
0251 
0252     KImageMapEditor::ToolType _currentToolType;
0253     AreaList *areas;
0254     AreaSelection *currentSelected;
0255     AreaSelection *copyArea;
0256     Area *defaultArea;
0257     DrawZone* drawZone;
0258     QTabWidget* tabWidget;
0259     AreaListView *areaListView;
0260     ImagesListView* imagesListView;
0261     MapsListView* mapsListView;
0262     HtmlMapElement* currentMapElement;
0263 
0264     //
0265     // Actions
0266     //
0267     KSelectAction* zoomAction;
0268     QAction *arrowAction;
0269     QAction *circleAction;
0270     QAction *rectangleAction;
0271     QAction *polygonAction;
0272     QAction *freehandAction;
0273     QAction *addPointAction;
0274     QAction *removePointAction;
0275 
0276     QAction *cutAction;
0277     QAction *deleteAction;
0278     QAction *copyAction;
0279     QAction *pasteAction;
0280     QAction *zoomInAction;
0281     QAction *zoomOutAction;
0282 
0283     QAction *mapNewAction;
0284     QAction *mapDeleteAction;
0285     QAction *mapNameAction;
0286     QAction *mapDefaultAreaAction;
0287 
0288     QAction *imageAddAction;
0289     QAction *imageRemoveAction;
0290     QAction *imageUsemapAction;
0291 
0292     KToggleAction *highlightAreasAction;
0293     KToggleAction *showAltAction;
0294 
0295     QAction *areaPropertiesAction;
0296 
0297     QAction *moveLeftAction;
0298     QAction *moveRightAction;
0299     QAction *moveUpAction;
0300     QAction *moveDownAction;
0301 
0302     QAction *increaseWidthAction;
0303     QAction *decreaseWidthAction;
0304     QAction *increaseHeightAction;
0305     QAction *decreaseHeightAction;
0306 
0307     QAction *toFrontAction;
0308     QAction *toBackAction;
0309     QAction *forwardOneAction;
0310     QAction *backOneAction;
0311 
0312     KToggleAction* configureShowAreaListAction;
0313     KToggleAction* configureShowMapListAction;
0314     KToggleAction* configureShowImageListAction;
0315 
0316 
0317     KRecentFilesAction* recentFilesAction;
0318 
0319     KXmlGuiWindow *mainWindow;
0320     QDockWidget* areaDock;
0321     QDockWidget* mapsDock;
0322     QDockWidget* imagesDock;
0323 
0324     QUndoStack *_commandHistory;
0325     int maxAreaPreviewHeight;
0326 
0327     QString cursorStatusText;
0328     QString selectionStatusText;
0329 
0330     void setupActions();
0331     void setupStatusBar();
0332     void updateStatusBar();
0333     /* refreshes all Areas, only used by preferences dialog
0334      * updates only the preview pictures*/
0335     void updateAllAreas();
0336     void updateUpDownBtn();
0337 
0338     QHash<QString,QString> getTagAttributes(QTextStream & s,QString &);
0339 
0340     void setMap(HtmlMapElement*);
0341     void setMap(MapTag*);
0342     void addMap(const QString &);
0343 
0344     // Returns the entire html file as a String
0345     HtmlElement* findHtmlElement(const QString &);
0346     HtmlImgElement* findHtmlImgElement(ImageTag*);
0347     HtmlMapElement* findHtmlMapElement(const QString &);
0348     void deleteAllMaps();
0349     void addImage(const QUrl &);
0350     void setImageActionsEnabled(bool);
0351     void setMapActionsEnabled(bool);
0352 
0353     void saveAreasToMapTag(MapTag*);
0354     void showPopupMenu(const QPoint &, const QString &);
0355     void drawToCenter(QPainter* p, const QString & str, int y, int width);
0356 
0357 public slots:
0358     bool openURL(const QUrl & url) override;
0359     void slotChangeStatusCoords(int x,int y);
0360     void slotUpdateSelectionCoords();
0361     void slotUpdateSelectionCoords( const QRect &);
0362     void slotAreaChanged(Area *);
0363     void slotShowMainPopupMenu(const QPoint &);
0364     void slotShowMapPopupMenu(const QPoint &);
0365     void slotShowImagePopupMenu(const QPoint &);
0366     void slotConfigChanged();
0367     void setPicture(const QUrl & url);
0368     void setMap(const QString &);
0369     void setMapName(const QString & s);
0370 
0371 
0372 protected slots:
0373     // overridden from KReadWritePart
0374     bool openFile() override;
0375 
0376     bool saveFile() override {
0377         saveImageMap( url() );
0378 //        setModified(false);
0379         return true;
0380     }
0381 
0382     void fileOpen();
0383     void fileSaveAs();
0384     void fileSave();
0385     void fileClose();
0386 
0387     void slotShowPopupMenu(const QPoint &);
0388     void slotShowPreferences();
0389     void slotHighlightAreas(bool b);
0390     void slotShowAltTag(bool b);
0391     void slotSelectionChanged();
0392 
0393     int showTagEditor(QTreeWidgetItem *item);
0394     int showTagEditor();
0395 
0396     void slotZoom();
0397     void slotZoomIn();
0398     void slotZoomOut();
0399 
0400     void slotCut();
0401     void slotCopy();
0402     void slotPaste();
0403     void slotDelete();
0404 
0405     void slotDrawArrow();
0406     void slotDrawCircle();
0407     void slotDrawRectangle();
0408     void slotDrawPolygon();
0409     void slotDrawFreehand();
0410     void slotDrawAddPoint();
0411     void slotDrawRemovePoint();
0412 
0413     void mapDefaultArea();
0414     void mapNew();
0415     void mapDelete();
0416     void mapEditName();
0417     void mapShowHTML();
0418     void mapPreview();
0419 
0420     void slotBackOne();
0421     void slotForwardOne();
0422     void slotToBack();
0423     void slotToFront();
0424 
0425     void slotMoveUp();
0426     void slotMoveDown();
0427     void slotMoveLeft();
0428     void slotMoveRight();
0429 
0430     void slotIncreaseHeight();
0431     void slotDecreaseHeight();
0432     void slotIncreaseWidth();
0433     void slotDecreaseWidth();
0434 
0435     void slotCancelDrawing();
0436 
0437     //  void slotPreferences();
0438     void imageAdd();
0439     void imageRemove();
0440     void imageUsemap();
0441 
0442 };
0443 
0444 
0445 inline KImageMapEditor::ToolType KImageMapEditor::currentToolType() const {
0446     return _currentToolType;
0447 }
0448 
0449 inline QString KImageMapEditor::mapName() const {
0450     return _mapName;
0451 }
0452 
0453 inline QUndoStack* KImageMapEditor::commandHistory() const {
0454     return _commandHistory;
0455 }
0456 
0457 #endif // IMAGEMAPEDITOR_H