1 /***************************************************************************
2  *   Copyright (C) 2005 by Sébastien Laoût                                 *
3  *   slaout@linux62.org                                                    *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 #ifndef TAGEDIT_H
22 #define TAGEDIT_H
23 
24 #include <QDialog>
25 #include <QtCore/QList>
26 #include <QItemDelegate>
27 #include <QTreeWidgetItem>
28 
29 #include "tag.h"
30 
31 class QCheckBox;
32 class QFontComboBox;
33 class QGroupBox;
34 class QHBoxLayout;
35 class QLabel;
36 class QLineEdit;
37 class QTreeWidget;
38 
39 class QKeyEvent;
40 class QMouseEvent;
41 
42 class KIconButton;
43 class QPushButton;
44 class QKeySequence;
45 class KShortcutWidget;
46 
47 class FontSizeCombo;
48 class KColorCombo2;
49 
50 class StateCopy
51 {
52 public:
53     typedef QList<StateCopy*> List;
54     StateCopy(State *old = 0);
55     ~StateCopy();
56     State *oldState;
57     State *newState;
58     void copyBack();
59 };
60 
61 class TagCopy
62 {
63 public:
64     typedef QList<TagCopy*> List;
65     TagCopy(Tag *old = 0);
66     ~TagCopy();
67     Tag *oldTag;
68     Tag *newTag;
69     StateCopy::List stateCopies;
70     void copyBack();
71     bool isMultiState();
72 };
73 
74 class TagListViewItem : public QTreeWidgetItem
75 {
76     friend class TagListDelegate;
77 public:
78     TagListViewItem(QTreeWidget *parent, TagCopy *tagCopy);
79     TagListViewItem(QTreeWidgetItem *parent, TagCopy *tagCopy);
80     TagListViewItem(QTreeWidget *parent, QTreeWidgetItem *after, TagCopy *tagCopy);
81     TagListViewItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, TagCopy *tagCopy);
82     TagListViewItem(QTreeWidget *parent, StateCopy *stateCopy);
83     TagListViewItem(QTreeWidgetItem *parent, StateCopy *stateCopy);
84     TagListViewItem(QTreeWidget *parent, QTreeWidgetItem *after, StateCopy *stateCopy);
85     TagListViewItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, StateCopy *stateCopy);
86     ~TagListViewItem();
tagCopy()87     TagCopy*   tagCopy()   {
88         return m_tagCopy;
89     }
stateCopy()90     StateCopy* stateCopy() {
91         return m_stateCopy;
92     }
93     bool isEmblemObligatory();
94     TagListViewItem* lastChild();
95     TagListViewItem* prevSibling();
96     TagListViewItem* nextSibling();
97     TagListViewItem* parent() const; // Reimplemented to cast the return value
98     int width(const QFontMetrics &fontMetrics, const QTreeWidget *listView, int column) const;
99     void setup();
100 
101 private:
102     TagCopy   *m_tagCopy;
103     StateCopy *m_stateCopy;
104 };
105 
106 Q_DECLARE_METATYPE(TagListViewItem*);
107 
108 class TagListView : public QTreeWidget
109 {
110     Q_OBJECT
111 public:
112     TagListView(QWidget *parent = 0);
113     ~TagListView();
114     void keyPressEvent(QKeyEvent *event);
115     void mouseDoubleClickEvent(QMouseEvent *event);
116     void mousePressEvent(QMouseEvent *event);
117     void mouseReleaseEvent(QMouseEvent *event);
118     TagListViewItem* currentItem() const; // Reimplemented to cast the return value
119     TagListViewItem* firstChild() const; // Reimplemented to cast the return value
120     TagListViewItem* lastItem() const; // Reimplemented to cast the return value
121 signals:
122     void deletePressed();
123     void doubleClickedItem();
124 };
125 
126 /**
127   * @author Sébastien Laoût
128   */
129 class TagsEditDialog : public QDialog
130 {
131     Q_OBJECT
132 public:
133     explicit TagsEditDialog(QWidget *parent = 0, State *stateToEdit = 0, bool addNewTag = false);
134     ~TagsEditDialog();
deletedStates()135     State::List deletedStates() {
136         return m_deletedStates;
137     }
addedStates()138     State::List addedStates()   {
139         return m_addedStates;
140     }
141     TagListViewItem* itemForState(State *state);
142 
143 private slots:
144     void newTag();
145     void newState();
146     void moveUp();
147     void moveDown();
148     void deleteTag();
149     void renameIt();
150     void capturedShortcut(const QKeySequence &shortcut);
151     void removeShortcut();
152     void removeEmblem();
153     void modified();
154     void currentItemChanged(QTreeWidgetItem *item, QTreeWidgetItem* next = 0);
155     void slotCancel();
156     void slotOk();
157     void selectUp();
158     void selectDown();
159     void selectLeft();
160     void selectRight();
161     void resetTreeSizeHint();
162 private:
163     void loadBlankState();
164     void loadStateFrom(State *state);
165     void loadTagFrom(Tag *tag);
166     void saveStateTo(State *state);
167     void saveTagTo(Tag *tag);
168     void ensureCurrentItemVisible();
169     TagListView   *m_tags;
170     QPushButton   *m_moveUp;
171     QPushButton   *m_moveDown;
172     QPushButton   *m_deleteTag;
173     QLineEdit     *m_tagName;
174     KShortcutWidget *m_shortcut;
175     QPushButton   *m_removeShortcut;
176     QCheckBox     *m_inherit;
177     QGroupBox     *m_tagBox;
178     QHBoxLayout   *m_tagBoxLayout;
179     QGroupBox     *m_stateBox;
180     QHBoxLayout   *m_stateBoxLayout;
181     QLabel        *m_stateNameLabel;
182     QLineEdit     *m_stateName;
183     KIconButton   *m_emblem;
184     QPushButton   *m_removeEmblem;
185     QPushButton   *m_bold;
186     QPushButton   *m_underline;
187     QPushButton   *m_italic;
188     QPushButton   *m_strike;
189     KColorCombo2  *m_textColor;
190     QFontComboBox *m_font;
191     FontSizeCombo *m_fontSize;
192     KColorCombo2  *m_backgroundColor;
193     QLineEdit     *m_textEquivalent;
194     QCheckBox     *m_onEveryLines;
195     QCheckBox     *m_allowCrossRefernce;
196 
197     TagCopy::List m_tagCopies;
198     State::List   m_deletedStates;
199     State::List   m_addedStates;
200 
201     bool m_loading;
202 };
203 
204 
205 class TagListDelegate : public QItemDelegate
206 {
207     Q_OBJECT
208 
209 public:
QItemDelegate(parent)210     TagListDelegate(QWidget *parent = 0) : QItemDelegate(parent) {}
211     void paint(QPainter *painter, const QStyleOptionViewItem &option,
212                const QModelIndex &index) const;
213 
214 };
215 
216 #endif // TAGEDIT_H
217