Debian Codegraph @

src/widgetlayout.h

raw symbols SCIP
1 /*
2 Copyright (C) 2010 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #ifndef WIDGETLAYOUT_H
24 #define WIDGETLAYOUT_H
25
26 #include <QtGui>
27
28 #define QCS_MAX_EVENTS 4096
29 #define QCS_CURVE_BUFFER_MAX 4096
30
31 #include "qutewidget.h"
32 #include "curve.h"
33 #include "widgetpreset.h"
34
35 class QuteConsole;
36 class QuteGraph;
37 class QuteScope;
38 class QuteButton;
39 class FrameWidget;
40 class QuteTable;
41
42 class RegisteredController {
43 public:
44 RegisteredController(QuteWidget * _widget, int _chan,int _cc) {
45 widget = _widget;
46 chan = _chan;
47 cc = _cc;
48 }
49 int chan;
50 int cc;
51 QuteWidget * widget;
52 };
53
54 class WidgetLayout : public QWidget
55 {
56 Q_OBJECT
57 Q_PROPERTY(bool openProperties READ getOpenProperties WRITE setOpenProperties) // To make sure only one properties dialog is displayed at any one time
58 public:
59 WidgetLayout(QWidget* parent);
60 ~WidgetLayout();
61 // unsigned int widgetCount();
62 // void loadWidgets(QString widgets);
63 void loadXmlWidgets(QString xmlWidgets);
64 void loadXmlPresets(QString xmlPresets);
65 void loadMacWidgets(QString macWidgets);
66 QString getWidgetsText(); // With full tags
67 QString getPresetsText();
68 QString getSelectedWidgetsText();
69 QString getMacWidgetsText(); // With full tags
70 QStringList getSelectedMacWidgetsText();
71 QString getCsladspaLines();
72 QString getCabbageWidgets();
73 bool openMidiPort(int port);
74 void closeMidiPort();
75 QVector<QuteWidget *> getWidgets() {return m_widgets;}
76
77 // Data to/from widgets
78 void setValue(QString channelName, double value);
79 void setValue(QString channelName, QString value);
80 void setValue(int index, double value);
81 void setValue(int index, QString value);
82 QString getStringForChannel(QString channelName, bool *modified = 0);
83 double getValueForChannel(QString channelName, bool *modified = 0, double notfound = 0.0);
84 void getMouseValues(QVector<double> *values);
85 int getMouseX();
86 int getMouseY();
87 int getMouseRelX();
88 int getMouseRelY();
89 int getMouseBut1();
90 int getMouseBut2();
91 void setWidgetProperty(QString widgetid, QString property, QVariant value);
92 QVariant getWidgetProperty(QString widgetid, QString property);
93 void flush();
94
95 // Behavior
96 void setKeyRepeatMode(bool repeat);
97 void showWidgetTooltips(bool show);
98 void setWidgetToolTip(QuteWidget *widget, bool show);
99 void setContained(bool contained);
100 void setCurrentPosition(QPoint pos); // To set the mouse position for new widgets
101 void setFontOffset(double offset);
102 void setFontScaling(double scaling);
103 void setWidgetsLocked(bool lock);
104 void setUpdateRate(int rate) { m_updateRate = rate; }
105
106 // Properties
107 bool getOpenProperties() { return m_openProperties; }
108 void setOpenProperties(bool open) {m_openProperties = open; }
109 // Outer geometry refers to the geometry of the parent panel if not separate window
110 void setOuterGeometry(QRect r);
111 QRect getOuterGeometry();
112
113 // void getValues(QVector<QString> *channelNames,
114 // QVector<double> *values,
115 // QVector<QString> *stringValues);
116
117 bool uuidFree(QString uuid);
118 int newXmlWidget(QDomNode node, bool offset = false, bool newId = false);
119 QString newMacWidget(QString widgetLine, bool offset = false); // Offset is used when pasting duplicated widgets
120 void registerWidget(QuteWidget *widget);
121
122 // TEST: try to get midiBingins info
123 QString getMidiControllerInstrument();
124
125 QVector<QVector<int> > midiQueue;
126 int midiWriteCounter;
127 int midiReadCounter;
128
129 // Notifiations
130 void engineStopped(); // To let the widgets know engine has stopped (to free unused curve buffers)
131
132 // Preset methods
133 void setPresetName(int num, QString name);
134 QList<int> getPresetNums();
135 QString getPresetName(int num);
136 bool presetExists(int num);
137
138 // Curves from API
139 void appendCurve(WINDAT *windat);
140 void killCurve(WINDAT *windat);
141 void newCurve(Curve* curve);
142 void setCurveData(Curve *curve);
143 uintptr_t getCurveById(uintptr_t id);
144 void updateCurve(WINDAT *windat);
145 int killCurves(CSOUND *csound);
146 void clearGraphs(); // This also frees the memory allocated by curves.
147 void flushGraphBuffer();
148
149 void refreshWidgets();
150 bool isModified();
151 // void passWidgetClipboard(QString text);
152
153 void createContextMenu(QContextMenuEvent *event); // When done outside container widget
154
155 // Edition Actions (Local edit actions without keyboard shortcuts as the main application handles that)
156 QAction *cutAct;
157 QAction *copyAct;
158 QAction *pasteAct;
159 QAction *reloadWidgetsAct;
160 QAction *clearAct;
161 QAction *selectAllAct;
162 QAction *duplicateAct;
163 QAction *deleteAct;
164 QAction *propertiesAct;
165
166 // Create new widget Actions
167 QAction *createSliderAct;
168 QAction *createLabelAct;
169 QAction *createDisplayAct;
170 QAction *createScrollNumberAct;
171 QAction *createLineEditAct;
172 QAction *createSpinBoxAct;
173 QAction *createButtonAct;
174 QAction *createKnobAct;
175 QAction *createCheckBoxAct;
176 QAction *createMenuAct;
177 QAction *createMeterAct;
178 QAction *createConsoleAct;
179 QAction *createGraphAct;
180 QAction *createScopeAct;
181 QAction *createTableDisplayAct;
182
183 // Alignment Actions
184 QAction *alignLeftAct;
185 QAction *alignRightAct;
186 QAction *alignTopAct;
187 QAction *alignBottomAct;
188 QAction *sendToBackAct;
189 QAction *sendToFrontAct;
190 QAction *distributeHorizontalAct;
191 QAction *distributeVerticalAct;
192 QAction *alignCenterHorizontalAct;
193 QAction *alignCenterVerticalAct;
194
195 // Preset actions
196 QAction *storePresetAct;
197 QAction *newPresetAct;
198 QAction *recallPresetAct;
199
200 // Value changes buffer to store all value changes from widgets that
201 // have been triggered from the GUI
202 QHash<QString, double> newValues;
203 QHash<QString, QString> newStringValues;
204 QMutex valueMutex;
205 QMutex stringValueMutex;
206 QReadWriteLock mouseLock;
207
208 QString getQml();
209
210 QuteWidgetType widgetNameToType(QString widgetName);
211 void addCreateWidgetActionsToMenu(QMenu &menu);
212
213 public slots:
214 QString createNewLabel(int x = -1, int y = -1, QString channel = QString());
215 QString createNewDisplay(int x = -1, int y = -1, QString channel = QString());
216 QString createNewScrollNumber(int x = -1, int y = -1, QString channel = QString());
217 QString createNewLineEdit(int x = -1, int y = -1, QString channel = QString());
218 QString createNewSpinBox(int x = -1, int y = -1, QString channel = QString());
219 QString createNewSlider(int x = -1, int y = -1, QString channel = QString());
220 QString createNewButton(int x = -1, int y = -1, QString channel = QString());
221 QString createNewKnob(int x = -1, int y = -1, QString channel = QString());
222 QString createNewCheckBox(int x = -1, int y = -1, QString channel = QString());
223 QString createNewMenu(int x = -1, int y = -1, QString channel = QString());
224 QString createNewMeter(int x = -1, int y = -1, QString channel = QString());
225 QString createNewConsole(int x = -1, int y = -1, QString channel = QString());
226 QString createNewGraph(int x = -1, int y = -1, QString channel = QString());
227 QString createNewScope(int x = -1, int y = -1, QString channel = QString());
228 QString createNewTableDisplay(int x= -1, int y= -1, QString channel = QString());
229
230 void clearWidgets();
231 void clearWidgetLayout();
232
233
234 QStringList getUuids();
235 QStringList listProperties(QString widgetid); // widgetid can be eihter uuid (prefered) or channel
236 bool destroyWidget(QString widgetid);
237
238 void propertiesDialog();
239 void applyProperties();
240 void selectBgColor();
241 void setEditEnabled(bool enabled);
242 void setEditMode(bool active);
243 void deselectAll();
244 void selectAll();
245 void widgetMoved(QPair<int, int>);
246 void widgetResized(QPair<int, int>);
247 void mousePressEventParent(QMouseEvent *event); // To receive events from ancestors when outside the widget area
248 void mouseReleaseEventParent(QMouseEvent *event);
249 void mouseMoveEventParent(QMouseEvent *event);
250 void selectionChanged(QRect selection);
251 QSize getUsedSize();
252 void adjustLayoutSize();
253 void adjustWidgetSize();
254 void setModified(bool mod = true);
255 void setMouseOffset(int x, int y);
256
257 // Preset slots
258 void loadPreset(); // Show dialog and ask
259 void loadPresetFromAction(); // Triggered from menu item
260 void loadPresetFromItem(QTreeWidgetItem * item, int column); // Triggered from tree widget
261 void loadPreset(int num);
262 void loadPresetFromIndex(int index);
263 void newPreset(); // Show dialog asking for name
264 // void newPreset(int num, QString name);
265 void savePreset(); // Show dialog asking for name
266 void savePreset(int num, QString name);
267
268 // Editing
269 void copy();
270 void cut();
271 void paste();
272 // void paste(QPoint pos);
273 void duplicate();
274 void deleteSelected();
275 void moveSelected(int horiz, int vert, int grid=1);
276 void undo();
277 void redo();
278 void reloadWidgets();
279
280 // Alignment
281 void alignLeft();
282 void alignRight();
283 void alignTop();
284 void alignBottom();
285 void sendToBack();
286 void sendToFront();
287 void distributeHorizontal();
288 void distributeVertical();
289 void alignCenterVertical();
290 void alignCenterHorizontal();
291
292 void markHistory();
293 void createEditFrame(QuteWidget* widget);
294
295 void widgetChanged(QuteWidget* widget = 0);
296 void deleteWidget(QuteWidget *widget);
297
298 void newValue(QPair<QString, double> channelValue);
299 void newValue(QPair<QString, QString> channelValue);
300 void processNewValues();
301 void queueEvent(QString eventLine);
302
303 void processUpdateCurve(Curve *curve);
304 // Messages
305 void appendMessage(QString message);
306
307
308 protected:
309 virtual void mousePressEvent(QMouseEvent *event);
310 virtual void mouseMoveEvent(QMouseEvent *event);
311 virtual void mouseReleaseEvent(QMouseEvent *event);
312 virtual void keyPressEvent(QKeyEvent *event);
313 virtual void keyReleaseEvent(QKeyEvent *event);
314 virtual void contextMenuEvent(QContextMenuEvent *event);
315 virtual void closeEvent(QCloseEvent *event);
316 QRubberBand *selectionFrame;
317 int startx, starty;
318
319 private:
320
321 QMutex widgetsMutex;
322 QMutex layoutMutex;
323 QList<Curve *> newCurveBuffer; // To store curves from Csound for widget panel Graph widgets
324 QVector<WINDAT> curveUpdateBuffer; // FIXME move these buffers to documentpage to avoid duplication when having multiple panels
325 int curveUpdateBufferCount;
326 QList<Curve *> curves;
327 QTimer updateTimer;
328
329 unsigned long m_ksmpscount; // Ksmps counter for Csound engine (Really needed here?)
330
331 // Properties of the panel (saved to xml file)
332 int m_posx, m_posy, m_w, m_h; // Position and size of panel (not this widget)
333 QString m_objectName;
334 QString m_uuid;
335 bool m_visible;
336
337 // Configuration
338 bool m_repeatKeys;
339 bool m_xmlFormat;
340 bool m_trackMouse;
341 bool m_openProperties; // Open widget properties when creating widgets
342 bool m_enableEdit; // Enable editing and properties dialog
343 bool m_tooltips; // Show widget tooltips
344 int mouseX, mouseY, mouseRelX, mouseRelY, mouseBut1, mouseBut2;
345 int xOffset, yOffset;
346 double m_fontOffset, m_fontScaling;
347 int m_updateRate;
348
349 // For the properties dialog - they store the configuration data for the widget panel
350 QPoint currentPosition; //TODO use proper variables instead of storing data in the widgets...
351 QCheckBox *bgCheckBox;
352 QPushButton *bgButton;
353 int closing; // to control timer when destroying this object
354 // XXXX: flag to control updateData
355 bool m_updating;
356
357 QVector<QString> m_history; // Undo/ Redo history
358 int m_historyIndex; // Current point in history
359 bool m_modified;
360 bool m_editMode;
361 // QString m_clipboard;
362 // Whether contained in another widget (scrollbar in widget panel or widget panel)
363 bool m_contained;
364
365 QVector<WidgetPreset> presets;
366 int m_currentPreset; // If -1 no current preset
367
368 QList<RegisteredController> registeredControllers;
369
370 // Contained Widgets
371 QVector<QuteWidget *> m_widgets;
372 QVector<FrameWidget *> editWidgets;
373 // These vectors must be used with care since they are not reentrant and will
374 // cause problems when accessed simultaneously
375 // They are pointers to widgets already in widgets vector
376 // TODO check where these are accessed for problems
377 QVector<QuteConsole *> consoleWidgets;
378 QVector<QuteGraph *> graphWidgets;
379 QVector<QuteScope *> scopeWidgets;
380 int m_activeWidgets; // Keeps a number of widgets that can be currently accessed by value callbacks (e.g. set to 0 during paste). This is done to avoid locking the callbacks, which are called from a realtime thread
381
382 int parseXmlNode(QDomNode node);
383 QString createSlider(int x, int y, int width, int height, QString widgetLine);
384 QString createText(int x, int y, int width, int height, QString widgetLine);
385 QString createScrollNumber(int x, int y, int width, int height, QString widgetLine);
386 QString createLineEdit(int x, int y, int width, int height, QString widgetLine);
387 QString createSpinBox(int x, int y, int width, int height, QString widgetLine);
388 QString createButton(int x, int y, int width, int height, QString widgetLine);
389 QString createKnob(int x, int y, int width, int height, QString widgetLine);
390 QString createCheckBox(int x, int y, int width, int height, QString widgetLine);
391 QString createMenu(int x, int y, int width, int height, QString widgetLine);
392 QString createMeter(int x, int y, int width, int height, QString widgetLine);
393 QString createConsole(int x, int y, int width, int height, QString widgetLine);
394 QString createGraph(int x, int y, int width, int height, QString widgetLine);
395 QString createScope(int x, int y, int width, int height, QString widgetLine);
396 QString createDummy(int x, int y, int width, int height, QString widgetLine);
397 QString createTableDisplay(int x, int y, int width, int height, QString widgetLine);
398
399
400 void setBackground(bool bg, QColor bgColor);
401 FrameWidget *getEditWidget(QuteWidget *widget);
402
403 void registerWidgetController(QuteWidget *widget, int cc);
404 void registerWidgetChannel(QuteWidget *widget, int chan);
405 void unregisterWidgetController(QuteWidget *widget);
406 void clearWidgetControllers();
407
408 //Undo history
409 void clearHistory();
410
411 // Preset Methods
412 int getPresetIndex(int number);
413
414 //XML helper functions
415 QColor getColorFromElement(QDomElement elem); // Converts an XML color element to a QColor structure
416
417 QHash<QString, QuteWidgetType> m_widgetNameToType;
418
419 private slots:
420 void updateData();
421 void widgetSelected(QuteWidget *widget);
422 void widgetUnselected(QuteWidget *widget);
423
424 signals:
425 void selection(QRect area);
426 void keyPressed(int key);
427 void keyReleased(int key);
428 void changed(); // Should be triggered whenever widgets change, to let main document know
429 void registerScope(QuteScope *scope);
430 void registerGraph(QuteGraph *graph);
431 void registerButton(QuteButton *button);
432 void requestCsoundUserData(QuteWidget *widget);
433 void queueEventSignal(QString eventLine);
434 void widgetSelectedSignal(QuteWidget *widget);
435 void widgetUnselectedSignal(QuteWidget *widget);
436 void showMidiLearn(QuteWidget *);
437 void addChn_kSignal(QString channel);
438 void windowStatus(bool);
439 // void setWidgetClipboardSignal(QString text); // To propagate clipboard for sharing between pages
440 };
441
442 #endif // WIDGETLAYOUT_H