1 /***************************************************************************
2     Copyright (C) 2001-2009 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #ifndef TELLICO_ENTRYEDITDIALOG_H
26 #define TELLICO_ENTRYEDITDIALOG_H
27 
28 #include "observer.h"
29 #include "gui/fieldwidget.h"
30 
31 #include <QDialog>
32 #include <QHash>
33 
34 namespace Tellico {
35   namespace GUI {
36     class TabWidget;
37   }
38 
39 /**
40  * @author Robby Stephenson
41  */
42 class EntryEditDialog : public QDialog, public Observer {
43 Q_OBJECT
44 
45 // needed for completion object support
46 friend class GUI::FieldWidget;
47 
48 public:
49   EntryEditDialog(QWidget* parent);
50   ~EntryEditDialog();
51 
52   /**
53    * Checks to see if any data needs to be saved. Returns @p true if it's ok to continue with
54    * saving or closing the widget.
55    *
56    * @return Returns @p true if either the data has not been modified or the user to save or discard the new data.
57    */
58   bool queryModified();
59   /**
60    * Deletes and resets the layout of the tabs.
61    *
62    * @param coll A pointer to the collection whose fields should be used for setting up the layout
63    */
64   void resetLayout(Data::CollPtr coll);
65   /**
66    * Clears all of the input controls in the widget. The pointer to the
67    * current entry is nullified, but not the pointer to the current collection.
68    */
69   void clear();
70 
71   virtual void    addEntries(Data::EntryList entries) Q_DECL_OVERRIDE;
72   virtual void modifyEntries(Data::EntryList entries) Q_DECL_OVERRIDE;
73 
74   virtual void    addField(Data::CollPtr coll, Data::FieldPtr field) Q_DECL_OVERRIDE;
75   /**
76    * Updates a widget when its field has been modified. The category may have changed, completions may have
77    * been added or removed, or what-have-you.
78    *
79    * @param coll A pointer to the parent collection
80    * @param oldField A pointer to the old field, which should have the same name as the new one
81    * @param newField A pointer to the new field
82    */
83   virtual void modifyField(Data::CollPtr coll, Data::FieldPtr oldField, Data::FieldPtr newField) Q_DECL_OVERRIDE;
84   /**
85    * Removes a field from the editor.
86    *
87    * @param field The field to be removed
88    */
89   virtual void removeField(Data::CollPtr, Data::FieldPtr field) Q_DECL_OVERRIDE;
90 
91 public Q_SLOTS:
92   /**
93    * Called when the Close button is clicked. It just hides the dialog.
94    */
95   virtual void slotClose();
96   /**
97    * Resets the widget, deleting all of its contents
98    */
99   void slotReset();
100   /**
101    * Handles clicking the New button. The old entry pointer is destroyed and a
102    * new one is created, but not added to any collection.
103    */
104   void slotHandleNew();
105   /**
106    * Handles clicking the Save button. All the values in the entry widgets are
107    * copied into the entry object. @ref signalSaveEntry is made. The widget is cleared,
108    * and the first tab is shown.
109    */
110   void slotHandleSave();
111   /**
112    * This slot is called whenever anything is modified. It's public so I can call it
113    * from a @ref FieldEditWidget.
114    */
115   void slotSetModified(bool modified=true);
116   /**
117    * Sets the contents of the input controls to match the contents of a list of entries.
118    *
119    * @param list A list of the entries. The data in the first one will be inserted in the controls, and
120    * the widgets will be enabled or not, depending on whether the rest of the entries match the first one.
121    */
122   void setContents(Tellico::Data::EntryList entries);
123   /**
124    * Override the implementation to check whether the entry needs to be modified
125    */
126   virtual void reject() Q_DECL_OVERRIDE;
127 
128 protected Q_SLOTS:
129   void slotHelp();
130 
131 private Q_SLOTS:
132   void fieldValueChanged(Tellico::Data::FieldPtr field);
133   void fieldChanged(Tellico::Data::FieldPtr field);
134   void slotUpdateSize();
135 
136 private:
137   /**
138    * Sets the contents of the input controls to match the contents of a entry.
139    *
140    * @param entry A pointer to the entry
141    * @param highlight An optional string to highlight
142    */
143   void setEntry(Data::EntryPtr entry);
144   /**
145    * Updates the completion objects in the edit boxes to include values
146    * contained in a certain entry.
147    *
148    * @param entry A pointer to the entry
149    */
150   void updateCompletions(Data::EntryPtr entry);
151   virtual void showEvent(QShowEvent* event) Q_DECL_OVERRIDE;
152   virtual void hideEvent(QHideEvent* event) Q_DECL_OVERRIDE;
153   virtual void closeEvent(QCloseEvent* event) Q_DECL_OVERRIDE;
154 
155   Data::CollPtr m_currColl;
156   Data::EntryList m_currEntries;
157   GUI::TabWidget* m_tabs;
158   QHash<QString, GUI::FieldWidget*> m_widgetDict;
159 
160   QPushButton* m_newButton;
161   QPushButton* m_saveButton;
162 
163   bool m_modified;
164   Data::FieldList m_modifiedFields;
165   bool m_isOrphan;
166   bool m_isWorking;
167   bool m_needReset;
168 };
169 
170 } // end namespace
171 #endif
172