|
KEdit has quite a few menu items, so I'll only cover a small sample of
them so that you can get the flavor of how to do this.
ORIG: setupMenuBar()
file = new QPopupMenu();
file->insertItem(BarIcon("filenew"),i18n("&New..."),
this, SLOT(file_new()), KStdAccel::openNew());
file->insertItem(BarIcon("fileopen"), i18n("&Open..."),
this, SLOT(file_open()), KStdAccel::open());
menuBar()->insertItem(i18n("&File"), file);
| | |
This creates the file menu and inserts two items: the New item and
Open item. It then inserts the file menu into the menubar.
We want to delete these lines and replace them with the following:
NEW: setupActions()
KStdAction::openNew(this, SLOT(file_new()), actionCollection());
KStdAction::open(this, SLOT(file_open()), actionCollection());
| | |
That's it. You don't need to worry about which menus these actions
are inserted into or how they are represented visually. Just call the
static methods that correspond to your item and pass along your SLOT
and an action collection and you're good to go.
This is how you handle 90% of all standard items.
The Open Recent item deserves a little more special attention as it
is a special case. The following code shows the old way to do it:
recentpopup = new QPopupMenu();
file->insertItem(i18n("Open &Recent..."), recentpopup);
connect(recentpopup, SIGNAL(activated(int)), SLOT(openRecent(int)));
| | |
These three lines get replaces by the following:
recent = KStdAction::openRecent(this, SLOT(file_openRecent(const KURL&)),
actionCollection());
| | |
To load and save the entries, you need a KConfig pointer set to the
appropriate group. These are the functions to call:
recent->loadEntries(kc_ptr);
recent->saveEntries(kc_ptr);
| | |
We will come back to this later.
We continue travelling down the setupMenuBar() method and see
that most of the items are standard ones. We go until we see the
"Open URL" and "Save To URL" items. These are app specific.
file->insertItem(i18n("Open &URL..."), this, SLOT(file_open_url()));
file->insertItem(i18n("Save &To URL..."), this, SLOT(file_save_url()));
| | |
This is one time where the action replacement is actually longer then
the original.
(void)new KAction(i18n("Open &URL..."), 0, this, SLOT(file_open_url()),
actionCollection(), "file_open_url");
(void)new KAction(i18n("Save &To URL..."), 0, this, SLOT(file_save_url()),
actionCollection(), "save_to_url");
| | |
The internal names ("file_open_url" and "save_to_url") are somewhat
arbitrary, but you do have to remember what they are for later.
Another example I want to show is our toggle items.
options->setCheckable(TRUE);
toolID = options->insertItem(i18n("Show &Toolbar"), this,SLOT(toggleToolBar()));
statusID = options->insertItem(i18n("Show St&atusbar"), this,SLOT(toggleStatusBar()));
| | |
Remember also that these were checked by default in our constructor.
Well, we will replace these with:
KStdAction::showToolbar(this, SLOT(toggleToolBar()), actionCollection());
KStdAction::showStatusbar(this, SLOT(toggleStatusBar()), actionCollection());
| | |
The items will be checked by default without us having to write any
code. We'll see how the toggle methods change, later.
The final example I will show is the help menu.
QString about = i18n(""
"KEdit %1\n\n"
"Copyright 1997-98\n"
"Bernd Johannes Wuebben\n"
"wuebben@kde.org").arg(KEDITVERSION);
menuBar()->insertItem(i18n("&Help"), helpMenu(about) );
| | |
It is safe to just delete this without adding any replacement code.
The help menu is handled automatically behind the scenes.
In general, you can just delete the setupToolBar() method
totally.
All of the action creating was fine and dandy.. but it's also useless
if you forget to actually build the GUI. This is done with the
following method:
Some notes about this method:
- It must be called after you create your actions. It will not
work if it is called before you create them.
- Call this method only once.
- Make sure you make install your app, not just make it -
otherwise you won't see any effect to UI changes.
- The filename used is your app-specific XML file. If you don't specify
an absolute path, your app's data directory will be searched for the file.
- If you leave the file parameter blank, then a file called [app-name]ui.rc
will be used (so in our example, we have omitted the
filename since it is keditui.rc). If there is no file by that name,
only standard UI items will be built. This is handy only if you do not
have any app-specific actions.
But again, you must call the createGUI() method
after you create your actions or nothing will happen!
|