File: example.cpp

package info (click to toggle)
juffed 0.10-89-g3690b60-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,160 kB
  • sloc: cpp: 21,060; ansic: 446; xml: 329; sh: 68; makefile: 16
file content (130 lines) | stat: -rw-r--r-- 4,039 bytes parent folder | download | duplicates (5)
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
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>

#include "Constants.h"
#include "Log.h"
#include "SciDocEngine.h"
#include "Settings.h"
#include "Utils.h"

// This class will be catching signals from actions.
class SignalProcessor : public QObject {
Q_OBJECT
public:
	SignalProcessor() : QObject() {
		doc_ = NULL;
	}
	
	void setDocument(Juff::Document* doc) {
		doc_ = doc;
	}
	
public slots:
	
	// Of course it's gonna crash if the doc_ is not initialized yet.
	// I've omitted extra checks for simplicity - it's just an example.
	
	void slotCut()   { doc_->cut();   }
	void slotCopy()  { doc_->copy();  }
	void slotPaste() { doc_->paste(); }
	void slotUndo()  { doc_->undo();  }
	void slotRedo()  { doc_->redo();  }

private:
	Juff::Document* doc_;
};

int main(int argc, char* argv[]) {
	QApplication app(argc, argv);
	
	// Read JuffEd's settings
	Settings::instance()->read("juff", "juffed");
	
	// Create an object of the class defined abofe
	// that will catch signals from actions and call 
	// corresponding methods from a document.
	// It's a single document in this example but it can
	// be multiple documents in your case so you need to
	// keep track of the currently active document.
	SignalProcessor proc;
	
	// Initialize some standard commands (see Constants.h for 
	// more of them) and connect them to slots. You don't need 
	// to care about icons and shortcuts - it'll all be 
	// taken care of inside the CommandStorage class. It'll also
	// pick up custom shortcuts (if there are any) redefined in JuffEd.
	//
	// This needs to be done at the very beginning before any of 
	// documents were created (since the document populates its own
	// context menu using CommandStorage's actions).
	//
	CommandStorageInt* cmdStrg = Juff::Utils::commandStorage();
	cmdStrg->addAction(EDIT_CUT,   "Cut",   &proc, SLOT(slotCut()));
	cmdStrg->addAction(EDIT_COPY,  "Copy",  &proc, SLOT(slotCopy()));
	cmdStrg->addAction(EDIT_PASTE, "Paste", &proc, SLOT(slotPaste()));
	cmdStrg->addAction(EDIT_UNDO,  "Undo",  &proc, SLOT(slotUndo()));
	cmdStrg->addAction(EDIT_REDO,  "Redo",  &proc, SLOT(slotRedo()));
	
	
	// create an engine
	SciDocEngine* engine = new SciDocEngine();
	
	// Main Window
	QMainWindow mw;
	
	// Menus
	QMenu* editMenu = new QMenu("Edit");
	QMenu* viewMenu = new QMenu("View");
	// Add some standard actions we initialized above.
	// As per been said above, you don't need to care about
	// icons, shortcuts or slots - it's all been already 
	// taken care of.
	editMenu->addAction(cmdStrg->action(EDIT_CUT));
	editMenu->addAction(cmdStrg->action(EDIT_COPY));
	editMenu->addAction(cmdStrg->action(EDIT_PASTE));
	editMenu->addAction(cmdStrg->action(EDIT_UNDO));
	editMenu->addAction(cmdStrg->action(EDIT_REDO));
	editMenu->addSeparator();
	// An interesting part: getting engine-specific actions
	// and adding them to a menu. You don't need to take care
	// of slots - it's all done inside the engine.
	Juff::ActionList actions;
	actions = engine->mainMenuActions(Juff::MenuEdit);
	foreach (QAction* a, actions) {
		editMenu->addAction(a);
	}
	actions = engine->mainMenuActions(Juff::MenuView);
	foreach (QAction* a, actions) {
		viewMenu->addAction(a);
	}
	
	mw.menuBar()->addMenu(editMenu);
	mw.menuBar()->addMenu(viewMenu);
	
	// Toolbar
	QToolBar* tb = mw.addToolBar("main");
	tb->addAction(cmdStrg->action(EDIT_CUT));
	tb->addAction(cmdStrg->action(EDIT_COPY));
	tb->addAction(cmdStrg->action(EDIT_PASTE));
	tb->addAction(cmdStrg->action(EDIT_UNDO));
	tb->addAction(cmdStrg->action(EDIT_REDO));

	// Create a document
	Juff::Document* doc = engine->createDoc("example.cpp");
	// Notify the engine that this is the document that was activated
	engine->onDocActivated(doc);
	// Pass the document to the signal processor. Not it will not crash
	// when some of his slots are activated :)
	proc.setDocument(doc);
	
	
	// Set the document as a central widget and show the main window
	mw.setCentralWidget(doc);
	mw.resize(800, 600);
	mw.show();
	
	return app.exec();
}

#include "example.moc"