KDE UI Tutorial
Preface: Conventions
Step 0: Getting Started
Step 1: Initial Code
Step 2: Core Dump
   Old Setup
   New Setup
Step 2: Line by Line
Step 3: Detail... Details..
Step 4: App Specific Details

Additional Notes

Kurt Granroth <granroth@kde.org>

The Core Work

Now we get to the main part of our work -- converting the menu and toolbar building code to our new framework. I'll dump the original and converted methods here, now, and then go through a line by line comparison later:

These two functions, setupMenuBar and setupToolBar, were responsible for constructing the menubar and toolbar in the original kedit.

OLD: kedit.cpp
void TopLevel::setupMenuBar()
{
  file =    new QPopupMenu ();
  edit =    new QPopupMenu ();
  options = new QPopupMenu ();
  recentpopup = 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());

  file->insertItem(i18n("Open &Recent..."), recentpopup);
  connect( recentpopup, SIGNAL(activated(int)), SLOT(openRecent(int)) );

  file->insertSeparator(-1);

  file->insertItem(BarIcon("filefloppy"), i18n("&Save"),
                      this, SLOT(file_save()), KStdAccel::save());
  file->insertItem(i18n("Save &As..."),
                      this, SLOT(file_save_as()));
  file->insertItem(i18n("&Close"),
                      this, SLOT(file_close()), KStdAccel::close());
  file->insertSeparator (-1);
  file->insertItem(i18n("Open &URL..."),
                      this, SLOT(file_open_url()));
  file->insertItem(i18n("Save &To URL..."),
                      this,SLOT(file_save_url()));
  file->insertSeparator (-1);

  file->insertItem(BarIcon("fileprint"), i18n("&Print..."),
                      this, SLOT(print()), KStdAccel::print());

  file->insertSeparator (-1);
  file->insertItem(i18n("&Mail..."),
                      this, SLOT(mail()) );
  file->insertSeparator (-1);
  file->insertItem(BarIcon("exit"), i18n("&Quit"),
            this,    SLOT(close()), KStdAccel::quit());

  edit->insertItem(i18n("Und&o"), this,
           SLOT(undo()), KStdAccel::undo());
  edit->insertItem(i18n("Re&do"), this,
           SLOT(redo()), KStdAccel::redo());
  edit->insertSeparator(-1);
  edit->insertItem(BarIcon("editcut"), i18n("C&ut"),
           this,     SLOT(cut()));
  edit->insertItem(BarIcon("editcopy"), i18n("&Copy"),
           this,     SLOT(copy()));
  edit->insertItem(BarIcon("editpaste"), i18n("&Paste"),
           this,     SLOT(paste()));
  edit->insertItem(i18n("Select &All"),
           this,     SLOT(select_all()));
  edit->insertSeparator(-1);
  edit->insertItem(i18n("&Insert File"),
           this,     SLOT(file_insert()));
  edit->insertItem(i18n("I&nsert Date"),
           this,     SLOT(insertDate()));
  edit->insertItem(i18n("Cl&ean Spaces"), this, SLOT( clean_space()));
  edit->insertSeparator(-1);
  edit->insertItem(BarIcon("find"), i18n("&Find..."),
           this,     SLOT(search()), KStdAccel::find());
  edit->insertItem(i18n("Find A&gain"),
           this,     SLOT(search_again()));
  edit->insertItem(i18n("&Replace"),
           this,     SLOT(replace()), KStdAccel::replace());
  edit->insertItem(BarIcon("spellcheck"), i18n("&Spellcheck..."),
           this, SLOT(spellcheck()));


  edit->insertSeparator(-1);
  edit->insertItem(i18n("&Goto Line..."),
           this, SLOT(gotoLine()));

  options->setCheckable(TRUE);
  toolID   = options->insertItem(i18n("Show &Toolbar"),
                 this,SLOT(toggleToolBar()));
  statusID = options->insertItem(i18n("Show St&atusbar"),
                 this,SLOT(toggleStatusBar()));    
  options->insertSeparator(-1);
  options->insertItem(i18n("Save &Options"),
              this,     SLOT(save_options()));
  options->insertSeparator(-1);
  options->insertItem( i18n("&Configure %1...").arg(kapp->caption()), 
               this, SLOT(customize()) );


  menuBar()->insertItem (i18n("&File"), file);
  menuBar()->insertItem (i18n("&Edit"), edit);
  menuBar()->insertItem (i18n("&Options"), options);
  menuBar()->insertSeparator(-1);
  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) );
}

void TopLevel::setupToolBar()
{
  QPixmap pixmap;

  pixmap = BarIcon("filenew2");
  toolBar(0)->insertButton( pixmap, 0, SIGNAL(clicked()), 
                this, SLOT(file_new()), TRUE, 
                i18n("New Document"));

  pixmap = BarIcon("fileopen");
  toolBar(0)->insertButton( pixmap, 0, SIGNAL(clicked()), 
                this, SLOT(file_open()), TRUE, 
                i18n("Open Document") );

  pixmap = BarIcon("filefloppy");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(file_save()), TRUE, 
                i18n("Save Document") );

  pixmap = BarIcon("fileprint");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(print()), TRUE, 
                i18n("Print Document") );

  toolBar(0)->insertSeparator();

  pixmap = BarIcon("editcopy");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(copy()), TRUE, 
                i18n("Copy") );

  pixmap = BarIcon("editpaste");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(paste()), TRUE, 
                i18n("Paste") );

  pixmap = BarIcon("editcut");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(cut()), TRUE, 
                i18n("Cut") );

  toolBar(0)->insertSeparator();

  pixmap = BarIcon( "undo" );
  toolBar(0)->insertButton( pixmap, 0, SIGNAL(clicked()),
                this, SLOT(undo()), true,
                i18n("Undo") );

  pixmap = BarIcon( "redo" );
  toolBar(0)->insertButton( pixmap, 0, SIGNAL(clicked()),
                this, SLOT(redo()), true,
                i18n("Redo") );

  toolBar(0)->insertSeparator();

  pixmap = BarIcon("send");
  toolBar(0)->insertButton(pixmap, 0, SIGNAL(clicked()), 
               this, SLOT(mail()), TRUE, 
               i18n("Mail Document") );

  toolBar(0)->insertSeparator();

  pixmap = BarIcon("help");
  toolBar(0)->insertButton( pixmap, 0,SIGNAL(clicked()), 
                this, SLOT(helpselected()), TRUE, 
                i18n("Help") );

  toolBar(0)->setBarPos( KToolBar::Top );
}

Both of those methods are replaces with on method that does it all (using much few lines of code, though) -- setupActions

NEW: kedit.cpp
void TopLevel::setupActions()
{
    // setup File menu
    KStdAction::openNew(this, SLOT(file_new()), actionCollection());
    KStdAction::open(this, SLOT(file_open()), actionCollection());
    recenter = KStdAction::openRecent(this, SLOT(openRecent(int)), actionCollection());
    KStdAction::save(this, SLOT(file_save()), actionCollection());
    KStdAction::saveAs(this, SLOT(file_save_as()), actionCollection());
    KStdAction::close(this, SLOT(file_close()), actionCollection());
    KStdAction::print(this, SLOT(print()), actionCollection());
    KStdAction::mail(this, SLOT(mail()), actionCollection());
    (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");
    KStdAction::quit(this, SLOT(close()), actionCollection());

    // setup edit menu
    KStdAction::undo(this, SLOT(undo()), actionCollection());
    KStdAction::redo(this, SLOT(redo()), actionCollection());
    KStdAction::cut(this, SLOT(cut()), actionCollection());
    KStdAction::copy(this, SLOT(copy()), actionCollection());
    KStdAction::paste(this, SLOT(paste()), actionCollection());
    KStdAction::selectAll(this, SLOT(select_all()), actionCollection());
    KStdAction::find(this, SLOT(search()), actionCollection());
    KStdAction::findNext(this, SLOT(search_again()), actionCollection());
    KStdAction::replace(this, SLOT(replace()), actionCollection());

    (void)new KAction(i18n("&Insert File"), 0, this, SLOT(file_insert()),
                      actionCollection(), "insert_file");
    (void)new KAction(i18n("I&nsert Date"), 0, this, SLOT(insertDate()),
                      actionCollection(), "insert_date");
    (void)new KAction(i18n("Cl&ean Spaces"), 0, this, SLOT(clean_space()),
                      actionCollection(), "clean_spaces");

    // setup Tools menu
    KStdAction::spelling(this, SLOT(spellcheck()), actionCollection());

    // setup Go menu
    KStdAction::gotoLine(this, SLOT(gotoLine()), actionCollection());

    // setup Settings menu
    KStdAction::showToolbar(this, SLOT(toggleToolBar()), actionCollection());
    KStdAction::showStatusbar(this, SLOT(toggleStatusBar()), actionCollection());

    KStdAction::saveOptions(this, SLOT(save_options()), actionCollection());
    KStdAction::preferences(this, SLOT(customize()), actionCollection());

    createGUI();
}


<< Prev Next >>