File indexing completed on 2024-04-28 15:39:47

0001 // SPDX-FileCopyrightText: 2014-2022 The KPhotoAlbum Development Team
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 // The basic resizable QFrame has been shamelessly stolen from
0006 // http://qt-project.org/forums/viewthread/24104
0007 // Big thanks to Mr. kripton :-)
0008 
0009 #ifndef RESIZABLEFRAME_H
0010 #define RESIZABLEFRAME_H
0011 
0012 #include "Dialog.h"
0013 
0014 #include <kpabase/enums.h>
0015 
0016 #include <QFrame>
0017 #include <QTreeWidgetItem>
0018 
0019 class QMouseEvent;
0020 
0021 namespace AnnotationDialog
0022 {
0023 
0024 /**
0025  * @brief The ResizableFrame class represents a positionable tag in the annotation dialog.
0026  * It has two basic states: associated to a tag, and unassociated.
0027  *
0028  * An AreaTagSelectDialog provides the context menu to allow associating the ResizableFrame with a tag,
0029  * as well as removing the tag, or removing the area completely.
0030  *
0031  * If an area is removed, the associated tag is usually removed from the image as well.
0032  *
0033  * ## Styling
0034  * The frame is styled based on this state (see property \c associated).
0035  * The following styles are expected to be set for a proper appearance of ResizableFrame:
0036  *  - `AnnotationDialog--ResizableFrame`
0037  *  - `AnnotationDialog--ResizableFrame:hover`
0038  *  - `AnnotationDialog--ResizableFrame[associated="true"]`
0039  */
0040 class ResizableFrame : public QFrame
0041 {
0042     Q_OBJECT
0043     Q_PROPERTY(bool associated READ associated)
0044 
0045 public:
0046     explicit ResizableFrame(QWidget *parent = nullptr);
0047     ~ResizableFrame() override;
0048 
0049     void setActualCoordinates(QRect actualCoordinates);
0050     QRect actualCoordinates() const;
0051 
0052     void checkGeometry();
0053     void showContextMenu();
0054 
0055     void setDialog(Dialog *dialog);
0056     QPair<QString, QString> tagData() const;
0057     void removeTagData();
0058     void setTagData(QString category, QString tag, ChangeOrigin changeOrigin = ManualChange);
0059     QPair<QString, QString> proposedTagData() const;
0060     void removeProposedTagData();
0061 
0062     /**
0063      * @brief Add the context menu actions to a QMenu.
0064      * @sa AreaTagSelectDialog
0065      * @param menu
0066      */
0067     void addTagActions(QMenu *menu);
0068     void markTidied();
0069     bool isTidied() const;
0070 
0071     /**
0072      * @brief associated
0073      * @return \c true, if a tag is associated with this area, \c false otherwise.
0074      */
0075     bool associated() const;
0076 
0077 protected:
0078     void mousePressEvent(QMouseEvent *event) override;
0079     void mouseMoveEvent(QMouseEvent *event) override;
0080     void mouseReleaseEvent(QMouseEvent *event) override;
0081     void contextMenuEvent(QContextMenuEvent *) override;
0082     /**
0083      * @brief repolish tells the widget to reevaluate its style.
0084      * This required when the style is dynamically changed because a property changed.
0085      */
0086     void repolish();
0087 
0088 private Q_SLOTS:
0089     void associateTag();
0090     void associateTag(QAction *action);
0091     void remove();
0092     void removeTag();
0093 
0094 private: // Functions
0095     void getMinMaxCoordinates();
0096     QAction *createAssociateTagAction(
0097         const QPair<QString, QString> &tag, QString prefix = QString());
0098 
0099 private: // Variables
0100     QPoint m_dragStartPosition;
0101     QRect m_dragStartGeometry;
0102     QRect m_minMaxCoordinates;
0103     QRect m_actualCoordinates;
0104     QAction *m_removeAct;
0105     QAction *m_removeTagAct;
0106     Dialog *m_dialog;
0107     QPair<QString, QString> m_tagData;
0108     QPair<QString, QString> m_proposedTagData;
0109     ImagePreview *m_preview;
0110     ImagePreviewWidget *m_previewWidget;
0111     bool m_tidied = false;
0112 };
0113 
0114 }
0115 
0116 #endif // RESIZABLEFRAME_H
0117 
0118 // vi:expandtab:tabstop=4 shiftwidth=4: