Skip to content
This repository
Browse code

Example update

  • Loading branch information...
commit 7d0c4b839c584f515389eaa1f5cd33e11a06f05b 1 parent f3a37ef
authored March 24, 2011
121  example/example.cpp
@@ -2,45 +2,34 @@
2 2
 #include <QMainWindow>
3 3
 #include <QMenuBar>
4 4
 
5  
-#include "SciDocEngine.h"
6  
-#include "Utils.h"
7 5
 #include "Constants.h"
8 6
 #include "Log.h"
  7
+#include "SciDocEngine.h"
  8
+#include "Settings.h"
  9
+#include "Utils.h"
9 10
 
10  
-// "signal processor" class
  11
+// This class will be catching signals from actions.
11 12
 class SignalProcessor : public QObject {
12 13
 Q_OBJECT
13 14
 public:
14 15
 	SignalProcessor() : QObject() {
15 16
 		doc_ = NULL;
16 17
 	}
17  
-
  18
+	
18 19
 	void setDocument(Juff::Document* doc) {
19 20
 		doc_ = doc;
20 21
 	}
21 22
 	
22 23
 public slots:
23 24
 	
24  
-	void slotCut() {
25  
-		doc_->cut();
26  
-	}
27  
-	
28  
-	void slotCopy() {
29  
-		doc_->copy();
30  
-	}
31  
-	
32  
-	void slotPaste() {
33  
-		doc_->paste();
34  
-	}
35  
-	
36  
-	void slotUndo() {
37  
-		LOGGER;
38  
-		doc_->undo();
39  
-	}
  25
+	// Of course it's gonna crash if the doc_ is not initialized yet.
  26
+	// I've omitted extra checks for simplicity - it's just an example.
40 27
 	
41  
-	void slotRedo() {
42  
-		doc_->redo();
43  
-	}
  28
+	void slotCut()   { doc_->cut();   }
  29
+	void slotCopy()  { doc_->copy();  }
  30
+	void slotPaste() { doc_->paste(); }
  31
+	void slotUndo()  { doc_->undo();  }
  32
+	void slotRedo()  { doc_->redo();  }
44 33
 
45 34
 private:
46 35
 	Juff::Document* doc_;
@@ -49,43 +38,62 @@ public slots:
49 38
 int main(int argc, char* argv[]) {
50 39
 	QApplication app(argc, argv);
51 40
 	
  41
+	// Read JuffEd's settings
  42
+	Settings::instance()->read("juff", "juffed");
  43
+	
  44
+	// Create an object of the class defined abofe
  45
+	// that will catch signals from actions and call 
  46
+	// corresponding methods from a document.
  47
+	// It's a single document in this example but it can
  48
+	// be multiple documents in your case so you need to
  49
+	// keep track of the currently active document.
52 50
 	SignalProcessor proc;
53  
-
54  
-	// create an engine
55  
-	SciDocEngine* engine = new SciDocEngine();
56  
-
57  
-	// Initialize some standard commands (see Constants.h for more of them)
58  
-	// and connect them to slots.
  51
+	
  52
+	// Initialize some standard commands (see Constants.h for 
  53
+	// more of them) and connect them to slots. You don't need 
  54
+	// to care about icons and shortcuts - it'll all be 
  55
+	// taken care of inside the CommandStorage class. It'll also
  56
+	// pick up custom shortcuts (if there are any) redefined in JuffEd.
59 57
 	//
60 58
 	// This needs to be done at the very beginning before any of 
61 59
 	// documents were created (since the document populates its own
62 60
 	// context menu using CommandStorage's actions).
63 61
 	//
64  
-	CommandStorageInt* st = Juff::Utils::commandStorage();
65  
-	st->addAction(EDIT_CUT,   "Cut",   &proc, SLOT(slotCut()));
66  
-	st->addAction(EDIT_COPY,  "Copy",  &proc, SLOT(slotCopy()));
67  
-	st->addAction(EDIT_PASTE, "Paste", &proc, SLOT(slotPaste()));
68  
-	st->addAction(EDIT_UNDO,  "Undo",  &proc, SLOT(slotUndo()));
69  
-	st->addAction(EDIT_REDO,  "Redo",  &proc, SLOT(slotRedo()));
70  
-		
  62
+	CommandStorageInt* cmdStrg = Juff::Utils::commandStorage();
  63
+	cmdStrg->addAction(EDIT_CUT,   "Cut",   &proc, SLOT(slotCut()));
  64
+	cmdStrg->addAction(EDIT_COPY,  "Copy",  &proc, SLOT(slotCopy()));
  65
+	cmdStrg->addAction(EDIT_PASTE, "Paste", &proc, SLOT(slotPaste()));
  66
+	cmdStrg->addAction(EDIT_UNDO,  "Undo",  &proc, SLOT(slotUndo()));
  67
+	cmdStrg->addAction(EDIT_REDO,  "Redo",  &proc, SLOT(slotRedo()));
  68
+	
  69
+	
  70
+	// create an engine
  71
+	SciDocEngine* engine = new SciDocEngine();
71 72
 	
72 73
 	// Main Window
73 74
 	QMainWindow mw;
74  
-
75  
-	// menu
  75
+	
  76
+	// Menus
76 77
 	QMenu* editMenu = new QMenu("Edit");
77  
-	editMenu->addAction(st->action(EDIT_CUT));
78  
-	editMenu->addAction(st->action(EDIT_COPY));
79  
-	editMenu->addAction(st->action(EDIT_PASTE));
80  
-	editMenu->addAction(st->action(EDIT_UNDO));
81  
-	editMenu->addAction(st->action(EDIT_REDO));
  78
+	QMenu* viewMenu = new QMenu("View");
  79
+	// Add some standard actions we initialized above.
  80
+	// As per been said above, you don't need to care about
  81
+	// icons, shortcuts or slots - it's all been already 
  82
+	// taken care of.
  83
+	editMenu->addAction(cmdStrg->action(EDIT_CUT));
  84
+	editMenu->addAction(cmdStrg->action(EDIT_COPY));
  85
+	editMenu->addAction(cmdStrg->action(EDIT_PASTE));
  86
+	editMenu->addAction(cmdStrg->action(EDIT_UNDO));
  87
+	editMenu->addAction(cmdStrg->action(EDIT_REDO));
82 88
 	editMenu->addSeparator();
83  
-	Juff::ActionList actions = engine->mainMenuActions(Juff::MenuEdit);
  89
+	// An interesting part: getting engine-specific actions
  90
+	// and adding them to a menu. You don't need to take care
  91
+	// of slots - it's all done inside the engine.
  92
+	Juff::ActionList actions;
  93
+	actions = engine->mainMenuActions(Juff::MenuEdit);
84 94
 	foreach (QAction* a, actions) {
85 95
 		editMenu->addAction(a);
86 96
 	}
87  
-	
88  
-	QMenu* viewMenu = new QMenu("View");
89 97
 	actions = engine->mainMenuActions(Juff::MenuView);
90 98
 	foreach (QAction* a, actions) {
91 99
 		viewMenu->addAction(a);
@@ -93,26 +101,25 @@ int main(int argc, char* argv[]) {
93 101
 	
94 102
 	mw.menuBar()->addMenu(editMenu);
95 103
 	mw.menuBar()->addMenu(viewMenu);
96  
-
97 104
 	
98  
-	// toolbar
  105
+	// Toolbar
99 106
 	QToolBar* tb = mw.addToolBar("main");
100  
-	tb->addAction(st->action(EDIT_CUT));
101  
-	tb->addAction(st->action(EDIT_COPY));
102  
-	tb->addAction(st->action(EDIT_PASTE));
103  
-	tb->addAction(st->action(EDIT_UNDO));
104  
-	tb->addAction(st->action(EDIT_REDO));
  107
+	tb->addAction(cmdStrg->action(EDIT_CUT));
  108
+	tb->addAction(cmdStrg->action(EDIT_COPY));
  109
+	tb->addAction(cmdStrg->action(EDIT_PASTE));
  110
+	tb->addAction(cmdStrg->action(EDIT_UNDO));
  111
+	tb->addAction(cmdStrg->action(EDIT_REDO));
105 112
 
106  
-	
107 113
 	// Create a document
108 114
 	Juff::Document* doc = engine->createDoc("example.cpp");
109  
-	// notify the engine that this is the document that was activated
  115
+	// Notify the engine that this is the document that was activated
110 116
 	engine->onDocActivated(doc);
111  
-	// set the document to the signal processor
  117
+	// Pass the document to the signal processor. Not it will not crash
  118
+	// when some of his slots are activated :)
112 119
 	proc.setDocument(doc);
113 120
 	
114 121
 	
115  
-	// put the document there and show the main window
  122
+	// Set the document as a central widget and show the main window
116 123
 	mw.setCentralWidget(doc);
117 124
 	mw.resize(800, 600);
118 125
 	mw.show();
6  example/example.pro
@@ -2,3 +2,9 @@ TEMPLATE      = app
2 2
 INCLUDEPATH  += . ../src/app/qsci ../include
3 3
 LIBS         += -ljuffed-engine-qsci -ljuff
4 4
 SOURCES      += example.cpp
  5
+
  6
+win32 {
  7
+	CONFIG += release
  8
+	CONFIG -= debug
  9
+	LIBS += -L.
  10
+}

0 notes on commit 7d0c4b8

Please sign in to comment.
Something went wrong with that request. Please try again.