CHAPTER 4. DEVELOPING APPLICATIONS
4.6 TextEditor, a complicated example
TextEditor has following functions.
- It selects and edits ".txt" extension.
- Apply appropriate Menu and Toolbar to user.
- It can enable/disable InputMethod.
TextEditor is composed as following classes.
- class ListView - class for file select screen, E.g.: list.h list.cpp
- class Editor - class for file edit screen, E.g.: editor.h editor.cpp
- class TextEditor- MainWindow, E.g.: texteditor.h texteditor.cpp
A. Class TextEditor
It partly connects Action to View by using child class of MzMainWindow.
TextEditor::TextEditor ()
: MzMainWindow (0)
{
/*
* It connects class ListView to openList () action
* that makes application run first time.
*/
connectAction (MzStdAction::openList (), MZ_RUNTIME_VIEW (ListView));
/*
* It connects class Editor to openWith()
* when file open () is asked. In other words, edit file
* offered from class Editor.
*/
connectAction (MzStdAction::openWith (), MZ_RUNTIME_VIEW (Editor));
/*
* It connects class Editor to openNew () action.
* It's used when you want to edit new file.
*/
connectAction (MzStdAction::openNew (), MZ_RUNTIME_VIEW (Editor));
}
|
B. Class ListView
ListView creates a Toolbar including "New" Icon.
It displays file list having ".txt" extension to screen using MzFileListView.
Following code in list.cpp shows constructor of ListView and
activatedView () function which makes Toolbar enable.
MZ_IMPLEMENT_DYNAVIEW (ListView); ListView::ListView (MzMainWindow *mainWindow) : MzView (mainWindow) { MzToolBarAction *toolbar; /* * It creates toolbar named "list_toolbar". */ toolbar = new MzToolBarAction (actionCollection (), "list_toolbar"); /* * It adds openNew () to toolbar. */ toolbar->insert (MzStdAction::openNew ()); /* * It creates Layout to arrange child widget. */ layout_ = new QVBoxLayout (this); layout_->setAutoAdd (true); /* * It searches mime type which extension is ".txt". */ MzMimeType mime = MzMimeFactory::instance ()->query ("txt"); /* * It creates class MzFileListView with the mime type. */ fileView_ = new MzFileListView (mime, this); fileView_->show (); connect (fileView_, SIGNAL (selectedFile (const QString &)), SLOT (selectedFile (const QString &))); } void ListView::activatedView () { /* * It makes menu disable. */ mainWindow ()->mainMenuControl ()->setMenuBar (0); /* * It uses toolbar named "list_toolbar". */ mainWindow ()->mainToolBarControl ()->setToolBar ( (MzToolBarAction *) actionCollection ()->action ("list_toolbar")); }
|
C. Class Editor
Editor opens and edits existing file when received openWith
() action. In contrast it edits new file when received openNew
() action. It also provides various menus related with editing.
MZ_IMPLEMENT_DYNAVIEW (Editor); Editor::Editor (MzMainWindow *mainWindow) : MzView (mainWindow) { MzMenuBarAction *menu; /* * It creates new menubar. */ menu = new MzMenuBarAction (actionCollection (), "editor_menu"); MzPopupAction *popup; /* * It creates File popup. */ popup = new MzPopupAction ("File", actionCollection (), "file_popup"); popup->insert (MzStdAction::openNew ()); popup->insert (MzStdAction::open ()); popup->insert (MzStdAction::save ()); popup->insert (MzStdAction::close ()); menu->insert (popup); /* * It creates Edit popup. */ popup = new MzPopupAction ("Edit", actionCollection (), "edit_popup"); popup->insert (MzStdAction::selectAll ()); popup->insert (MzStdAction::deselect ()); popup->insert (MzStdAction::find ()); menu->insert (popup); /* * It creates Help popup. */ popup = new MzPopupAction ("Help", actionCollection (), "help_popup"); popup->insert (MzStdAction::help ()); popup->insert (MzStdAction::aboutApp ()); popup->insert (MzStdAction::aboutLinuette ()); menu->insert (popup); layout_ = new QVBoxLayout (this); layout_->setAutoAdd (true); editor_ = new QMultiLineEdit (this); connect (MzStdAction::openWith (), SIGNAL (activated (const QString &)), SLOT (slotOpenWith (const QString &))); connect (MzStdAction::openNew (), SIGNAL (activated (const QString &)), SLOT (slotOpenNew ())); connect (MzStdAction::save (), SIGNAL (activated (const QString &)), SLOT (slotSave ())); } |
|