1 /*******************************************************************
2 
3 Part of the Fritzing project - http://fritzing.org
4 Copyright (c) 2007-2014 Fachhochschule Potsdam - http://fh-potsdam.de
5 
6 Fritzing is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 Fritzing is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with Fritzing.  If not, see <http://www.gnu.org/licenses/>.
18 
19 ********************************************************************
20 
21 $Revision: 6904 $:
22 $Author: irascibl@gmail.com $:
23 $Date: 2013-02-26 16:26:03 +0100 (Di, 26. Feb 2013) $
24 
25 ********************************************************************/
26 
27 
28 #ifndef BINMANAGER_H_
29 #define BINMANAGER_H_
30 
31 #include <QStackedWidget>
32 #include <QTabWidget>
33 #include <QMenu>
34 #include <QLabel>
35 #include <QDir>
36 
37 class ModelPart;
38 class PaletteModel;
39 class MainWindow;
40 class PartsBinPaletteWidget;
41 
42 struct BinLocation {
43 	enum Location {
44 		More,
45 		User,
46 		App,
47 		Outside,
48 		Any
49 	};
50 
51 	QString path;
52 	QString title;
53 	Location location;
54 	bool marked;
55 
56 	static QString toString(BinLocation::Location location);
57 	static BinLocation::Location findLocation(const QString & filename);
58 	static BinLocation::Location fromString(const QString & locationString);
59 };
60 
61 class BinManager : public QFrame {
62 	Q_OBJECT
63 	public:
64 		BinManager(class ReferenceModel *referenceModel, class HtmlInfoView *infoView, class WaitPushUndoStack *undoStack, MainWindow* parent);
65 		virtual ~BinManager();
66 
67         void initStandardBins();
68         void addBin(PartsBinPaletteWidget* bin);
69 		void insertBin(PartsBinPaletteWidget* bin, int index);
70 		void addPartToBin(ModelPart *modelPart, int position = -1);
71 		void addToMyParts(ModelPart *modelPart);
72 		void addToTempPartsBin(ModelPart *modelPart);
73 		void hideTempPartsBin();
74 
75 		void addNewPart(ModelPart *modelPart);
76 
77 		bool beforeClosing();
78 
79 		bool hasAlienParts();
80         QString createIfMyPartsNotExists();
81         QString createIfSearchNotExists();
82         QString createIfBinNotExists(const QString & dest, const QString & source);
83 
84 		void setInfoViewOnHover(bool infoViewOnHover);
85 		void load(const QString&);
86 
87 		void setDirtyTab(PartsBinPaletteWidget* w, bool dirty=true);
88 		void updateTitle(PartsBinPaletteWidget* w, const QString& newTitle);
89 
90 		void openBin(const QString &fileName);
91 		PartsBinPaletteWidget* openBinIn(QString fileName, bool fastLoad);
92 		PartsBinPaletteWidget* openCoreBinIn();
93 		void closeBinIn(int index);
94 
95 		void addPartTo(PartsBinPaletteWidget* bin, ModelPart* mp, bool setDirty);
96 		void editSelectedPartNewFrom(PartsBinPaletteWidget* bin);
97 
98 		const QString &getSelectedModuleIDFromSketch();
99 		QList<QAction*> openedBinsActions(const QString &moduleId);
100 
101 		MainWindow* mainWindow();
102         void search(const QString & searchText);
103 		bool currentViewIsIconView();
104 		void updateViewChecks(bool iconView);
105 		QMenu * binContextMenu(PartsBinPaletteWidget *);
106 		QMenu * partContextMenu(PartsBinPaletteWidget *);
107 		QMenu * combinedMenu(PartsBinPaletteWidget *);
108 		QMenu * combinedMenu();
109 		void setTabIcon(PartsBinPaletteWidget* w, QIcon *);
110 		void copyFilesToContrib(ModelPart *, QWidget * originator);
111 		void importPartToCurrentBin(const QString & filename);
112 		void importPartToMineBin(const QString & filename);
113         bool isTempPartsBin(PartsBinPaletteWidget * bin);
114         void setTempPartsBinLocation(const QString & filename);
115         void hideTabBar();
116 		void reloadPart(const QString & moduleID);
117 
118 	signals:
119 		void savePartAsBundled(const QString &moduleId);
120 
121 
122 	public slots:
123 		void updateBinCombinedMenuCurrent();
124 		void toIconView();
125 		void toListView();
126 		void setAsCurrentBin(PartsBinPaletteWidget* bin);
127 
128 	protected slots:
129 		void updateFileName(PartsBinPaletteWidget* bin, const QString &newFileName, const QString &oldFilename);
130 		void currentChanged(int);
131 		void tabCloseRequested(int);
132 		PartsBinPaletteWidget* newBinIn();
133 		void closeBin();
134 		void deleteBin();
135 		void editSelectedNew();
136 		void saveBin();
137 		void saveBinAs();
138 		void renameBin();
139         void copyToSketch();
140         void copyAllToSketch();
141 		void exportSelected();
142 		bool removeSelected();
143 		void findSelected();
144 		void saveBundledBin();
145         void mainLoad();
146 
147 	protected:
148 		void createMenu();
149 		PartsBinPaletteWidget* newBin();
150 		void registerBin(PartsBinPaletteWidget* bin);
151 		PartsBinPaletteWidget* getBin(int index);
152 		PartsBinPaletteWidget* currentBin();
153 		void setAsCurrentTab(PartsBinPaletteWidget* bin);
154         PartsBinPaletteWidget* getOrOpenMyPartsBin();
155         PartsBinPaletteWidget* getOrOpenSearchBin();
156         PartsBinPaletteWidget* getOrOpenBin(const QString & dest, const QString & source);
157         void connectTabWidget();
158 		void addPartToBinAux(PartsBinPaletteWidget *bin, ModelPart *modelPart, int position = -1);
159 		PartsBinPaletteWidget* findBin(const QString & binLocation);
160 		void createCombinedMenu();
161 		void createContextMenus();
162 		void saveStateAndGeometry();
163 		void restoreStateAndGeometry(QList<BinLocation *> & actualLocations);
164 		void findAllBins(QList<BinLocation *> & actualLocations);
165 		void findBins(QDir &, QList<BinLocation *> & actualLocations, BinLocation::Location);
166 		void readTheoreticalLocations(QList<BinLocation *> & theoreticalLocations);
167 		void updateBinCombinedMenu(PartsBinPaletteWidget * bin);
168         void importPart(const QString & filename, PartsBinPaletteWidget * bin);
169         void hackLocalContrib(QList<BinLocation *> &);
170 
171 
172 protected:
173 		ReferenceModel *m_referenceModel;
174 
175 		HtmlInfoView *m_infoView;
176 		WaitPushUndoStack *m_undoStack;
177 
178 		MainWindow *m_mainWindow;
179 		PartsBinPaletteWidget *m_currentBin;
180 		class StackTabWidget* m_stackTabWidget;
181 
182 		QHash<QString /*filename*/,PartsBinPaletteWidget*> m_openedBins;
183 		int m_unsavedBinsCount;
184 		QString m_defaultSaveFolder;
185 
186 		QMenu *m_binContextMenu;
187 		QMenu *m_combinedMenu;
188 
189 		QAction *m_openAction;
190 		QAction *m_newBinAction;
191 		QAction *m_closeBinAction;
192 		QAction *m_deleteBinAction;
193 		QAction *m_saveBinAction;
194 		QAction *m_saveBinAsAction;
195 		QAction *m_saveBinAsBundledAction;
196 		QAction *m_renameBinAction;
197 		QAction *m_copyToSketchAction;
198 		QAction *m_copyAllToSketchAction;
199 
200 		QAction *m_showListViewAction;
201 		QAction *m_showIconViewAction;
202 
203 		QMenu *m_partContextMenu;
204 		QMenu *m_partMenu;
205 		QAction *m_editPartNewAction;
206 		QAction *m_exportPartAction;
207 		QAction *m_removePartAction;
208 		QAction *m_findPartAction;
209 
210         QString m_tempPartsBinLocation;
211 
212 	protected:
213 		static QString StandardBinStyle;
214 		static QString CurrentBinStyle;
215 
216 	public:
217 		static QString Title;
218         static QString CorePartsBinLocation;
219         static QString MyPartsBinLocation;
220         static QString MyPartsBinTemplateLocation;
221         static QString SearchBinLocation;
222         static QString SearchBinTemplateLocation;
223 		static QString ContribPartsBinLocation;
224 		static QString TempPartsBinTemplateLocation;
225 		static QHash<QString, QString> StandardBinIcons;
226 
227 		static bool isTabReorderingEvent(QDropEvent* event);
228 		static void initNames();
229 		static bool getBinTitle(const QString & filename, QString & binTitle, QString & binIcon);
230 };
231 
232 #endif /* BINMANAGER_H_ */
233