Skip to content

Commit

Permalink
Example update
Browse files Browse the repository at this point in the history
  • Loading branch information
mezomish committed Mar 24, 2011
1 parent f3a37ef commit 7d0c4b8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 57 deletions.
121 changes: 64 additions & 57 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,34 @@
#include <QMainWindow>
#include <QMenuBar>

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

// "signal processor" class
// 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:

void slotCut() {
doc_->cut();
}

void slotCopy() {
doc_->copy();
}

void slotPaste() {
doc_->paste();
}

void slotUndo() {
LOGGER;
doc_->undo();
}
// 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 slotRedo() {
doc_->redo();
}
void slotCut() { doc_->cut(); }
void slotCopy() { doc_->copy(); }
void slotPaste() { doc_->paste(); }
void slotUndo() { doc_->undo(); }
void slotRedo() { doc_->redo(); }

private:
Juff::Document* doc_;
Expand All @@ -49,70 +38,88 @@ public slots:
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;

// create an engine
SciDocEngine* engine = new SciDocEngine();

// Initialize some standard commands (see Constants.h for more of them)
// and connect them to slots.
// 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* st = Juff::Utils::commandStorage();
st->addAction(EDIT_CUT, "Cut", &proc, SLOT(slotCut()));
st->addAction(EDIT_COPY, "Copy", &proc, SLOT(slotCopy()));
st->addAction(EDIT_PASTE, "Paste", &proc, SLOT(slotPaste()));
st->addAction(EDIT_UNDO, "Undo", &proc, SLOT(slotUndo()));
st->addAction(EDIT_REDO, "Redo", &proc, SLOT(slotRedo()));

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;

// menu
// Menus
QMenu* editMenu = new QMenu("Edit");
editMenu->addAction(st->action(EDIT_CUT));
editMenu->addAction(st->action(EDIT_COPY));
editMenu->addAction(st->action(EDIT_PASTE));
editMenu->addAction(st->action(EDIT_UNDO));
editMenu->addAction(st->action(EDIT_REDO));
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();
Juff::ActionList actions = engine->mainMenuActions(Juff::MenuEdit);
// 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);
}

QMenu* viewMenu = new QMenu("View");
actions = engine->mainMenuActions(Juff::MenuView);
foreach (QAction* a, actions) {
viewMenu->addAction(a);
}

mw.menuBar()->addMenu(editMenu);
mw.menuBar()->addMenu(viewMenu);


// toolbar
// Toolbar
QToolBar* tb = mw.addToolBar("main");
tb->addAction(st->action(EDIT_CUT));
tb->addAction(st->action(EDIT_COPY));
tb->addAction(st->action(EDIT_PASTE));
tb->addAction(st->action(EDIT_UNDO));
tb->addAction(st->action(EDIT_REDO));
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
// Notify the engine that this is the document that was activated
engine->onDocActivated(doc);
// set the document to the signal processor
// Pass the document to the signal processor. Not it will not crash
// when some of his slots are activated :)
proc.setDocument(doc);


// put the document there and show the main window
// Set the document as a central widget and show the main window
mw.setCentralWidget(doc);
mw.resize(800, 600);
mw.show();
Expand Down
6 changes: 6 additions & 0 deletions example/example.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ TEMPLATE = app
INCLUDEPATH += . ../src/app/qsci ../include
LIBS += -ljuffed-engine-qsci -ljuff
SOURCES += example.cpp

win32 {
CONFIG += release
CONFIG -= debug
LIBS += -L.
}

0 comments on commit 7d0c4b8

Please sign in to comment.