aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/classeditor/classeditor.h
blob: c04bd7da4712890625c6b6b2d6766b2b4840ab1e (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#ifndef _CLASSEDITOR_H_
#define _CLASSEDITOR_H_
//=============================================================================
//
//   File : classeditor.h
//   Creation date : Mon Feb 15 2010 14:35:55 CEST by Carbone Alessandro
//
//   This file is part of the KVIrc irc client distribution
//   Copyright (C) 2010 Alessandro Carbone (elfonol at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the linkss of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

/**
* \file classeditor.h
* \author Alessandro Carbone
* \brief Class Editor window
*/

#include "KviWindow.h"
#include "KviCString.h"
#include "KviPointerList.h"
#include "KviTalPopupMenu.h"
#include "KviHeapObject.h"
#include "KviKvsObjectClass.h"

#include <QWidget>
#include <QComboBox>
#include <QDialog>
#include <QLineEdit>
#include <QCheckBox>
#include <QTreeWidget>

class QLineEdit;
class QStringList;
class QPushButton;
class QCheckBox;
class QLabel;
class QSplitter;
class KviScriptEditor;

/**
* \class KviClassEditorTreeWidget
* \brief Describes the tree widget hierarchy
*/
class KviClassEditorTreeWidget : public QTreeWidget
{
	Q_OBJECT
public:
	/**
	* \brief Construct the tree widget
	* \param pParent The parent widget
	* \return KviClassEditorTreeWidget
	*/
	KviClassEditorTreeWidget(QWidget * pParent);
	
	/**
	* \brief Destroys the tree widget
	*/
	~KviClassEditorTreeWidget();
protected:
	virtual void mousePressEvent(QMouseEvent * e);
signals:
	/**
	* \brief Emitted when we press the mouse right button
	* \param pItem The item clicked
	* \param pos The position of the cursor
	* \return void
	*/
	void rightButtonPressed(QTreeWidgetItem * pItem, QPoint pos);
};

/**
* \class KviClassEditorTreeWidgetItem
* \brief Describes an item inside the class editor tree widget
*/
class KviClassEditorTreeWidgetItem : public QTreeWidgetItem, public KviHeapObject
{
public:
	/**
	* \enum Type
	* \brief Contains the type of the class item
	*/
	enum Type
	{
		Class,       /**< The item is a class */
		Namespace,   /**< The item is a namespace */
		Method       /**< The item is a member function */
	};
public:
	/**
	* \brief Constructs the class editor tree widget item
	* \param pTreeWidget The parent tree widget
	* \param eType The type of the item
	* \param szName The name of the item
	* \return KviClassEditorTreeWidgetItem
	*/
	KviClassEditorTreeWidgetItem(QTreeWidget * pTreeWidget, Type eType, const QString & szName);
	
	/**
	* \brief Constructs the class editor tree widget item
	* \param pParentItem The parent tree widget item
	* \param eType The type of the item
	* \param szName The name of the item
	* \return KviClassEditorTreeWidgetItem
	*/
	KviClassEditorTreeWidgetItem(KviClassEditorTreeWidgetItem * pParentItem, Type eType, const QString & szName);
	
	/**
	* \brief Destroys the class editor tree widget item
	*/
	~KviClassEditorTreeWidgetItem(){};
protected:
	Type    m_eType;
	QString m_szName;
	QString m_szBuffer;
	QString m_szInheritsClassName;
	QString m_szReminder;
	bool    m_bClassModified;
	bool    m_bInternal;
	int     m_iPos;
public:
	/**
	* \brief Sets the class name on which the class inherits from
	* \param szInheritsClassName The parent class name
	* \return void
	*/
	void setInheritsClass(QString szInheritsClassName){ m_szInheritsClassName = szInheritsClassName; };
	
	/**
	* \brief Returns the class name of the classes we inherit from
	* \return QString
	*/
	QString inheritsClass(){ return m_szInheritsClassName; };
	
	/**
	* \brief Returns the name of the item
	* \return const QString &
	*/
	const QString & name(){ return m_szName; };
	
	/**
	* \brief Sets the name of the item
	* \param szName The name of the item :P
	* \return void
	*/
	void setName(const QString & szName);
	
	/**
	* \brief Sets the list of parameters the function accept
	* \param szRem The parameters' list
	* \return void
	*/
	void setReminder(const QString & szRem){ m_szReminder = szRem; };
	
	/**
	* \brief Returns the parameters list
	* \return QString
	*/
	QString reminder(){ return m_szReminder; };

	/**
	* \brief Sets the state of the class as non-built
	* \param bModified Whether the class is modified
	* \return void
	*/
	void setClassNotBuilt(bool bModified);
	
	/**
	* \brief Returns true if we class is not built yet, false otherwise
	* \return bool
	*/
	bool classNotBuilt(){ return m_bClassModified; };
	
	/**
	* \brief Sets the function as internal
	* \param bInternal Whether the function is internal or not
	* \return void
	*/
	void setInternalFunction(bool bInternal){ m_bInternal = bInternal; };
	
	/**
	* \brief Returns true if the function is set as internal, false otherwise
	* \return bool
	*/
	bool isInternalFunction(){ return m_bInternal; };
	
	/**
	* \brief Returns the type of the item
	* \return Type
	*/
	Type type(){ return m_eType; };
	
	/**
	* \brief Sets the type of the item
	* \param eType The type!
	* \return void
	*/
	void setType(Type eType);
	
	/**
	* \brief Returns true if the item is a class, false otherwise
	* \return bool
	*/
	bool isClass(){ return m_eType == Class; };
	
	/**
	* \brief Returns true if the item is a namespace, false otherwise
	* \return bool
	*/
	bool isNamespace(){ return m_eType == Namespace; };
	
	/**
	* \brief Returns true if the item is a member function, false otherwise
	* \return bool
	*/
	bool isMethod(){ return m_eType == Method; };
	
	/**
	* \brief Saves the code of the item in a buffer
	* \param szBuffer The buffer ;)
	* \return void
	*/
	void setBuffer(const QString & szBuffer){ m_szBuffer = szBuffer; };
	
	/**
	* \brief Returns the buffer containing the code of the item
	* \return const QString &
	*/
	const QString & buffer(){ return m_szBuffer; };
	
	/**
	* \brief Returns the cursor's position
	* \return const int &
	*/
	const int & cursorPosition(){ return m_iPos; };

	/**
	* \brief Sets the cursor position
	* \param iPos The position of the cursor
	* \return void
	*/
	void setCursorPosition(const int & iPos){ m_iPos = iPos; };
};

/**
* \class KviClassEditor
* \brief The Class Editor module
*/
class KviClassEditor : public QWidget
{
	Q_OBJECT
public:
	/**
	* \brief Constructs the class editor object
	* \param pParent The parent widget
	* \return KviClassEditor
	*/
	KviClassEditor(QWidget * pParent);
	
	/**
	* \brief Destroys the class editor object
	*/
	~KviClassEditor();
public:
	KviScriptEditor              * m_pEditor;
	KviClassEditorTreeWidget     * m_pTreeWidget;
	QLabel                       * m_pClassNameLabel;
	QPushButton                  * m_pClassNameRenameButton;
	QLabel                       * m_pFunctionNameLabel;
	QPushButton                  * m_pFunctionNameRenameButton;
	QLabel                       * m_pReminderLabel;

	KviClassEditorTreeWidgetItem * m_pLastEditedItem;
	KviClassEditorTreeWidgetItem * m_pLastClickedItem;
	KviTalPopupMenu              * m_pContextPopup;
	QSplitter                    * m_pSplitter;
	QString                        m_szDir;
	bool                           m_bSaving;
protected:
	KviPointerHashTable<QString,KviClassEditorTreeWidgetItem> * m_pClasses;
public:
	/**
	* \brief Called to save the window properties
	* \param pCfg The configuration file
	* \return void
	*/
	virtual void saveProperties(KviConfigurationFile * pCfg);
	
	/**
	* \brief Called to load the window properties
	* \param pCfg The configuration file
	* \return void
	*/
	virtual void loadProperties(KviConfigurationFile * pCfg);
	
	/**
	* \brief Builds the class!
	* \return void
	*/
	void build();
	
	/**
	* \brief Saves non-built classes to a file
	* \return void
	*/
	void saveNotBuiltClasses();
	
	/**
	* \brief Loads non-built classes to a file
	* \return void
	*/
	void loadNotBuiltClasses();
	
	/**
	* \brief Searches a function
	* \param szFunctionName The name of the function
	* \param pClass The class in which the function is
	* \return KviClassEditorTreeWidgetItem *
	*/
	KviClassEditorTreeWidgetItem * findFunction(const QString & szFunctionName, KviClassEditorTreeWidgetItem * pClass);

	/**
	* \brief Exports class to a file
	* \param bSelectedOnly Whether to export only selected files
	* \param bSingleFiles Whether to export to single files
	* \return void
	*/
	void exportClasses(bool bSelectedOnly, bool bSingleFiles = false);
	
	/**
	* \brief Exports the current selection to single files
	* \param pList The list of file to export
	* \return void
	*/
	void exportSelectionInSinglesFiles(KviPointerList<KviClassEditorTreeWidgetItem> * pList);
	
	/**
	* \brief Removes the item from the tree
	* \param pItem The item to remove
	* \return void
	*/
	void cutItem(KviClassEditorTreeWidgetItem * pItem);
protected:
	//void recursiveSearchReplace(const QString &szSearch,KviClassEditorTreeWidgetItem * it,bool bReplace=false,const QString &szReplace="n");

	void exportClassBuffer(QString & szBuffer, KviClassEditorTreeWidgetItem * pItem);

	void oneTimeSetup();
	void createFullClass(KviKvsObjectClass * pClass, KviClassEditorTreeWidgetItem * pItem, const QString & szClassName);

	void saveLastEditedItem();
	void appendSelectedClassItems(KviPointerList<KviClassEditorTreeWidgetItem> * pList);
	void appendSelectedClassItemsRecursive(KviPointerList<KviClassEditorTreeWidgetItem> * pList, QTreeWidgetItem * pStartFrom);

	void appendAllClassItems(KviPointerList<KviClassEditorTreeWidgetItem> * pList);
	void appendAllClassItemsRecursive(KviPointerList<KviClassEditorTreeWidgetItem> * l,QTreeWidgetItem * pStartFrom);

	void openParentItems(QTreeWidgetItem * pItem);
	void activateItem(QTreeWidgetItem * pItem);
	bool hasSelectedItems();
	bool askForClassName(QString & szClassName, QString & szInheritsClassName, bool bEdit);
	bool askForNamespaceName(const QString & szAction, const QString & szText, const QString & szInitialText, QString & szNameBuffer);
	bool askForFunction(QString & szFunctionName, QString & szReminder, bool * pbInternal, const QString & szClassName, bool bRenameMode);
	void searchInheritedClasses(const QString szClass, KviPointerList<KviClassEditorTreeWidgetItem> & lInheritsedClasses);
	
	/**
	* \brief Returns true if the class exists, false otherwise
	* \param szName The name of the class
	* \return bool
	*/
	bool classExists(QString & szName);
	
	/**
	* \brief Renames a class
	* \param pClass The class to rename
	* \return void
	*/
	void renameClass(KviClassEditorTreeWidgetItem * pClass);
	
	/**
	* \brief Renames a namespace
	* \param pNamespace The namespace to rename
	* \return void
	*/
	void renameNamespace(KviClassEditorTreeWidgetItem * pNamespace);

	bool removeItem(KviClassEditorTreeWidgetItem * pItem, KviPointerList <KviClassEditorTreeWidgetItem> & lRemovedItems, bool * pbYesToAll);
	void updateClassHierarchy(KviClassEditorTreeWidgetItem * pClass);

	void removeItemChildren(KviClassEditorTreeWidgetItem * pItem, KviPointerList <KviClassEditorTreeWidgetItem> & lRemovedItems);
	void buildFullItemPath(KviClassEditorTreeWidgetItem * pItem, QString & szBuffer);
	QString buildFullClassName(KviClassEditorTreeWidgetItem * pItem);
	KviClassEditorTreeWidgetItem * createFullItem(const QString & szFullName);
	KviClassEditorTreeWidgetItem * createFullNamespace(const QString & szFullName);

	/**
	* \brief Searches an item
	* \param szName The name of the item to find
	* \return KviClassEditorTreeWidgetItem *
	*/
	KviClassEditorTreeWidgetItem * findItem(const QString & szName);
	
	
	
	KviClassEditorTreeWidgetItem * findTopLevelItem(const QString & szName);
	KviClassEditorTreeWidgetItem * createFullItemRecursive(QTreeWidgetItem * pCurrent, QStringList & lNameSpaces, int & iLevel, bool bCreateNameSpace = 0);
protected slots:
	void currentItemChanged(QTreeWidgetItem * pItem, QTreeWidgetItem *);
	void customContextMenuRequested(QPoint);
	void newClass();
	void newNamespace();
	void newMemberFunction();
	void renameFunction();
	KviClassEditorTreeWidgetItem * newItem(QString & szName, KviClassEditorTreeWidgetItem::Type eType);

	void renameItem();

	void exportAll();
	void exportSelectedSepFiles();
	void exportSelected();
	void slotFind();
	void slotFindWord(const QString &);
	void searchReplace(const QString & szSearch, bool bReplace = false, const QString & szReplace = "n");
	
	/**
	* \brief Called when we collapse an item
	* \param pItem The item to collapse
	* \return void
	*/
	void slotCollapseItems();
	
	/**
	* \brief Called when we collapse an item recursively
	* \param pItem The starting item to collapse
	* \return void
	*/
	void recursiveCollapseItems(KviClassEditorTreeWidgetItem * pItem);

	/**
	* \brief Called when we want to remove all selected items
	* \return void
	*/
	void removeSelectedItems();
	
	/**
	* \brief Called when we replace text everywhere
	* \param szBefore The text before the replace
	* \param szAfter The text after the replace
	* \return void
	*/
	void slotReplaceAll(const QString & szBefore, const QString & szAfter);
};

/**
* \class KviClassEditorWindow
* \brief This is the Class Editor window
*/
class KviClassEditorWindow : public KviWindow
{
	Q_OBJECT
public:
	/**
	* \brief Constructs the class editor window
	* \param pFrm The parent frame
	* \return KviClassEditorWindow
	*/
	KviClassEditorWindow(KviMainWindow * pFrm);
	
	/**
	* \brief Destroys the class editor window
	*/
	~KviClassEditorWindow();
protected:
	KviClassEditor * m_pEditor;
protected:
	/**
	* \brief Returns the class editor small icon
	* \return QPixmap *
	*/
	virtual QPixmap * myIconPtr();
	
	/**
	* \brief Sets the configuration group name as classeditor
	* \param szName The name of the group
	* \return void
	*/
	virtual void configGroupName(QString & szName);
	
	/**
	* \brief Called to save the window properties
	* \param pCfg The configuration file
	* \return void
	*/
	virtual void saveProperties(KviConfigurationFile * pCfg);
	
	/**
	* \brief Called to load the window properties
	* \param pCfg The configuration file
	* \return void
	*/
	virtual void loadProperties(KviConfigurationFile * pCfg);
protected slots:
	/**
	* \brief Called when we click the cancel button
	* \return void
	*/
	void cancelClicked();
	
	/**
	* \brief Called when we want to build the class
	* \return void
	*/
	void buildClicked();
	
	/**
	* \brief Called when we want to save the class
	* \return void
	*/
	void saveClicked();
};

/**
* \class KviClassEditorDialog
* \brief This class constructs the dialog to create a new class
*/
class KviClassEditorDialog: public QDialog
{
	Q_OBJECT
public:
	/**
	* \brief Constructs the class dialog
	* \param pParent The parent widget
	* \param szName The name of the widget
	* \param pClasses The container of all classes of our namespace
	* \param szClassName The name of the class
	* \param szInheritsClassName The name of the classes in which this inherits from
	* \param bRenameMode Whether the dialog is in rename mode
	* \return KviClassEditorDialog
	*/
	KviClassEditorDialog(QWidget * pParent, const QString & szName, KviPointerHashTable<QString,KviClassEditorTreeWidgetItem> * pClasses, const QString & szClassName, const QString & szInheritsClassName, bool bRenameMode = false);
	
	/**
	* \brief Destroys the class dialog
	*/
	~KviClassEditorDialog();
protected:
	QPushButton * m_pNewClassButton;
	QLineEdit   * m_pClassNameLineEdit;
	QComboBox   * m_pInheritsClassComboBox;
public:
	/**
	* \brief Returns the class name
	* \return QString
	*/
	QString className(){ return m_pClassNameLineEdit->text(); };
	
	/**
	* \brief Returns the class name of the parent classes
	* \return QString
	*/
	QString inheritsClassName(){ return m_pInheritsClassComboBox->currentText(); };
protected slots:
	/**
	* \brief Triggered when the name of the class change
	* \return void
	*/
	void textChanged(const QString &);
};

/**
* \class KviClassEditorFunctionDialog
* \brief This class constructs the dialog to create a new member function
*/
class KviClassEditorFunctionDialog: public QDialog
{
	Q_OBJECT
public:
	/**
	* \brief Constructs the function dialog
	* \param pParent The parent widget
	* \param szName The name of the widget
	* \param szClassName The name of the class
	* \param szFunctionName The name of the function
	* \param szReminder The parameters of the function
	* \param bIsInternal Whether the funtion is set as internal
	* \param bRenameMode Whether the dialog is in rename mode
	* \return KviClassEditorFunctionDialog
	*/
	KviClassEditorFunctionDialog(QWidget * pParent, const QString & szName, const QString & szClassName, const QString & szFunctionName, const QString & szReminder, bool bIsInternal, bool bRenameMode = false);
	
	/**
	* \brief Destroys the function dialog
	*/
	~KviClassEditorFunctionDialog();
protected:
	QPushButton * m_pNewFunctionButton;
	QLineEdit   * m_pFunctionNameLineEdit;
	QLineEdit   * m_pReminderLineEdit;
	QCheckBox   * m_pInternalCheckBox;
public:
	/**
	* \brief Returns the function name
	* \return QString
	*/
	QString functionName(){ return m_pFunctionNameLineEdit->text(); };
	
	/**
	* \brief Returns the parameters of the function
	* \return QString
	*/
	QString reminder(){ return m_pReminderLineEdit->text(); };

	/**
	* \brief Returns true if the function is set as internal, false otherwise
	* \return bool
	*/
	bool isInternalFunction(){ return m_pInternalCheckBox->isChecked(); };
protected slots:
	/**
	* \brief Triggered when the name of the function change
	* \return void
	*/
	void textChanged(const QString &);
};
#endif //_CLASSEDITOR_H_