File indexing completed on 2024-04-28 16:24:40

0001 /* This file is part of the KDE project
0002   Copyright (C) 2007 - 2010 Dag Andersen <danders@get2net.dk>
0003 
0004   This library is free software; you can redistribute it and/or
0005   modify it under the terms of the GNU Library General Public
0006   License as published by the Free Software Foundation; either
0007   version 2 of the License, or (at your option) any later version.
0008 
0009   This library is distributed in the hope that it will be useful,
0010   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012   Library General Public License for more details.
0013 
0014   You should have received a copy of the GNU Library General Public License
0015   along with this library; see the file COPYING.LIB.  If not, write to
0016   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017 * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KPTVIEWLIST_H
0021 #define KPTVIEWLIST_H
0022 
0023 #include "plan_export.h"
0024 
0025 #include "kptschedulemodel.h"
0026 
0027 #include <QTreeWidget>
0028 
0029 class QDomElement;
0030 
0031 class KoDocument;
0032 class KoView;
0033 
0034 class QComboBox;
0035 
0036 namespace KPlato
0037 {
0038 
0039 class View;
0040 class ViewBase;
0041 class ViewListItem;
0042 class ViewListWidget;
0043 
0044 class MainDocument;
0045 class Context;
0046 class ScheduleManager;
0047 
0048 #define TIP_USE_DEFAULT_TEXT "TIP_USE_DEFAULT_TEXT"
0049 
0050 //--------------
0051 struct ViewInfo
0052 {
0053     QString name;
0054     QString tip;
0055 };
0056 
0057 //--------------
0058 class PLAN_EXPORT ViewListItem : public QTreeWidgetItem
0059 {
0060     public:
0061         enum ItemType { ItemType_Category = Type, ItemType_SubView = UserType };
0062 
0063         enum DataRole { DataRole_View = Qt::UserRole, DataRole_Document };
0064 
0065         ViewListItem(const QString &tag, const QStringList &strings, int type = ItemType_Category);
0066         ViewListItem(QTreeWidget *parent, const QString &tag, const QStringList &strings, int type = ItemType_Category);
0067         ViewListItem(QTreeWidgetItem *parent, const QString &tag, const QStringList &strings, int type = ItemType_Category);
0068 
0069         void setView(ViewBase *view);
0070         ViewBase *view() const;
0071         void setDocument(KoDocument *doc);
0072         KoDocument *document() const;
0073 
0074         void setViewInfo(const ViewInfo &vi) { m_viewinfo = vi; }
0075         QString viewType() const;
0076         QString tag() const { return m_tag; }
0077         void save(QDomElement &element) const;
0078 
0079         void setReadWrite(bool rw);
0080 
0081     private:
0082         QString m_tag;
0083         ViewInfo m_viewinfo;
0084 };
0085 
0086 class PLAN_EXPORT ViewListTreeWidget : public QTreeWidget
0087 {
0088     Q_OBJECT
0089 public:
0090     explicit ViewListTreeWidget(QWidget *parent);
0091     ViewListItem *findCategory(const QString &cat);
0092 
0093     /// Return the category of @p view
0094     ViewListItem *category(const KoView *view) const;
0095 
0096     void save(QDomElement &element) const;
0097 
0098 protected:
0099     void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0100     void mousePressEvent (QMouseEvent *event) override;
0101     /// Setup drop enabled/disabled dependent on the selected item
0102     void startDrag(Qt::DropActions supportedActions) override;
0103     /// If modified by the drop, emit modified
0104     void dropEvent(QDropEvent *event) override;
0105 
0106 Q_SIGNALS:
0107     void activated(QTreeWidgetItem*);
0108     void updateViewInfo(KPlato::ViewListItem *itm);
0109     void modified();
0110 
0111 private Q_SLOTS:
0112     void handleMousePress(QTreeWidgetItem *item);
0113 };
0114 
0115 class PLAN_EXPORT ViewListWidget : public QWidget
0116 {
0117 Q_OBJECT
0118 public:
0119     ViewListWidget(MainDocument *part, QWidget *parent);//QString name, KXmlGuiWindow *parent);
0120     ~ViewListWidget() override;
0121 
0122     /// Set read/write permission on all views.
0123     void setReadWrite(bool rw);
0124     /// Add a category if it does not already exist
0125     ViewListItem *addCategory(const QString &tag, const QString& name);
0126     /// Return a list of all categories
0127     QList<ViewListItem*> categories() const;
0128 
0129     /// Return the category with @p tag
0130     ViewListItem *findCategory(const QString &tag) const;
0131     /// Return the category of @p view
0132     ViewListItem *category(const KoView *view) const;
0133 
0134     /// Create a unique tag
0135     QString uniqueTag(const QString &seed) const;
0136     /// Add a sub-view
0137     ViewListItem *addView(QTreeWidgetItem *category, const QString &tag, const QString &name, ViewBase *view, KoDocument *doc, const QString &iconName = QString(), int index = -1);
0138 
0139     void setSelected(QTreeWidgetItem *item);
0140     ViewListItem *currentItem() const;
0141     void setCurrentItem(QTreeWidgetItem *item);
0142     ViewListItem *currentCategory() const;
0143     KoView *findView(const QString &tag) const;
0144     ViewListItem *findItem(const QString &tag) const;
0145     ViewListItem *findItem(const QString &tag, QTreeWidgetItem* parent) const;
0146     ViewListItem *findItem(const ViewBase *view, QTreeWidgetItem* parent = 0) const;
0147 
0148     /// Remove @p item, don't emit signal
0149     int removeViewListItem(ViewListItem *item);
0150     /// Add @p item to @p parent at @p index, don't emit signal
0151     void addViewListItem(ViewListItem *item, QTreeWidgetItem *parent, int index);
0152 
0153     /// Remove @p item, emit signal
0154     int takeViewListItem(ViewListItem *item);
0155     /// Add @p item to @p parent at @ index, emit signal
0156     void insertViewListItem(ViewListItem *item, QTreeWidgetItem *parent, int index);
0157 
0158     void save(QDomElement &element) const;
0159 
0160     ViewListItem *previousViewItem() const { return m_prev; }
0161 
0162     ScheduleManager *selectedSchedule() const;
0163 
0164 Q_SIGNALS:
0165     void activated(KPlato::ViewListItem*, KPlato::ViewListItem*);
0166     void createView();
0167     void viewListItemRemoved(KPlato::ViewListItem *item);
0168     void viewListItemInserted(KPlato::ViewListItem *item, KPlato::ViewListItem *parent, int index);
0169 
0170     void selectionChanged(KPlato::ScheduleManager*);
0171 
0172     void updateViewInfo(KPlato::ViewListItem *itm);
0173 
0174     void modified();
0175 
0176 public Q_SLOTS:
0177     void setProject(KPlato::Project *project);
0178     void setSelectedSchedule(KPlato::ScheduleManager *sm);
0179     void setModified();
0180 
0181 protected Q_SLOTS:
0182     void slotActivated(QTreeWidgetItem *item, QTreeWidgetItem *prev);
0183     void slotItemChanged(QTreeWidgetItem *item, int col);
0184     void renameCategory();
0185     void slotAddView();
0186     void slotRemoveCategory();
0187     void slotRemoveView();
0188     void slotEditViewTitle();
0189     void slotEditDocumentTitle();
0190     void slotConfigureItem();
0191 
0192     void slotCurrentScheduleChanged(int);
0193     void slotScheduleManagerAdded(KPlato::ScheduleManager*);
0194 
0195     void slotDialogFinished(int result);
0196 
0197 protected:
0198     void contextMenuEvent (QContextMenuEvent *event) override;
0199 
0200 private:
0201     void setupContextMenus();
0202 
0203 private:
0204     MainDocument *m_part;
0205     ViewListTreeWidget *m_viewlist;
0206     QComboBox *m_currentSchedule;
0207     ScheduleSortFilterModel m_sfModel;
0208     ScheduleItemModel m_model;
0209 
0210     ViewListItem *m_contextitem;
0211     QList<QAction*> m_categoryactions;
0212     QList<QAction*> m_viewactions;
0213     QList<QAction*> m_listactions;
0214 
0215     ViewListItem *m_prev;
0216 
0217     ScheduleManager *m_temp;
0218 };
0219 
0220 } //Kplato namespace
0221 
0222 #endif