kandy Library API Documentation

kandy.cpp

00001 /*
00002 
00003  kandy.cpp
00004  
00005  Copyright (C) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00006 
00007 */
00008 
00009 #include <qdragobject.h>
00010 #include <qlineedit.h>
00011 #include <qprinter.h>
00012 #include <qprintdialog.h>
00013 #include <qpainter.h>
00014 #include <qpaintdevicemetrics.h>
00015 
00016 #include <kglobal.h>
00017 #include <klocale.h>
00018 #include <kiconloader.h>
00019 #include <kmenubar.h>
00020 #include <kkeydialog.h>
00021 #include <kaccel.h>
00022 #include <kio/netaccess.h>
00023 #include <kfiledialog.h>
00024 #include <kconfig.h>
00025 #include <kurl.h>
00026 #include <kurlrequesterdlg.h>
00027 #include <kdebug.h>
00028 #include <kmessagebox.h>
00029 #include <kstddirs.h>
00030 #include <kedittoolbar.h>
00031 #include <kstdaccel.h>
00032 #include <kaction.h>
00033 #include <kstdaction.h>
00034 
00035 #include "kandyprefsdialog.h"
00036 #include "commandscheduler.h"
00037 #include "kandyprefs.h"
00038 #include "modem.h"
00039 
00040 #include "kandy.h"
00041 #include <kstatusbar.h>
00042 #include "kandy.moc"
00043 
00044 Kandy::Kandy(CommandScheduler *scheduler)
00045     : KMainWindow( 0, "Kandy" ),
00046       mPrinter(0)
00047 {
00048   mScheduler = scheduler;
00049 
00050   mPreferencesDialog = 0;
00051 
00052   mView = new KandyView(mScheduler,this);
00053 
00054   // accept dnd
00055   setAcceptDrops(true);
00056 
00057   // tell the KMainWindow that this is indeed the main widget
00058   setCentralWidget(mView);
00059 
00060   // then, setup our actions
00061   setupActions();
00062 
00063   statusBar()->insertItem(i18n(" Disconnected "),0,0,true);
00064 
00065   // and a status bar
00066   statusBar()->show();
00067 
00068   // allow the view to change the statusbar and caption
00069   connect(mView, SIGNAL(signalChangeStatusbar(const QString&)),
00070           this,   SLOT(changeStatusbar(const QString&)));
00071   connect(mView, SIGNAL(signalChangeCaption(const QString&)),
00072           this,   SLOT(changeCaption(const QString&)));
00073 
00074   connect(mView,SIGNAL(modifiedChanged(bool)),SLOT(setTitle()));
00075 
00076   KConfig *config = KGlobal::config();
00077   config->setGroup("General");
00078   QString currentProfile = config->readEntry("CurrentProfile",
00079                                              locate("appdata","default.kandy"));
00080   if (!currentProfile.isEmpty()) load(currentProfile);
00081 }
00082 
00083 Kandy::~Kandy()
00084 {
00085 }
00086 
00087 void Kandy::load(const QString& filename)
00088 {
00089   if (!mView->loadFile(filename)) {
00090     KMessageBox::error(this,i18n("Could not load file %1").arg(filename));
00091   }
00092 
00093   mFilename = filename;
00094   setTitle();
00095 }
00096 
00097 void Kandy::save(const QString & filename)
00098 {
00099   if (!filename.isEmpty()) {
00100     if (!mView->saveFile(filename)) {
00101       KMessageBox::error(this,i18n("Couldn't save file %1.").arg(filename)); 
00102     } else {
00103       mFilename = filename;
00104       setTitle();
00105     }
00106   }
00107 }
00108 
00109 void Kandy::setupActions()
00110 {
00111   KStdAction::open(this, SLOT(fileOpen()), actionCollection());
00112   KStdAction::save(this, SLOT(fileSave()), actionCollection());
00113   KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
00114 //  KStdAction::print(this, SLOT(filePrint()), actionCollection());
00115   KStdAction::quit(this, SLOT(close()), actionCollection());
00116 
00117   mToolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
00118   mStatusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());
00119 
00120   KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
00121   KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
00122   KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
00123 
00124   new KAction(i18n("Mobile GUI"),0,this,SLOT(showMobileGui()),
00125               actionCollection(),"show_mobilegui");
00126 
00127   mConnectAction = new KAction(i18n("Connect"),0,this,SLOT(modemConnect()),
00128                                actionCollection(),"modem_connect");
00129   mDisconnectAction = new KAction(i18n("Disconnect"),0,this,
00130                                   SLOT(modemDisconnect()),actionCollection(),
00131                                   "modem_disconnect");
00132 
00133   createGUI();
00134 }
00135 
00136 void Kandy::saveProperties(KConfig */*config*/)
00137 {
00138     // the 'config' object points to the session managed
00139     // config file.  anything you write here will be available
00140     // later when this app is restored
00141 }
00142 
00143 void Kandy::readProperties(KConfig */*config*/)
00144 {
00145     // the 'config' object points to the session managed
00146     // config file.  this function is automatically called whenever
00147     // the app is being restored.  read in here whatever you wrote
00148     // in 'saveProperties'
00149 }
00150 
00151 void Kandy::dragEnterEvent(QDragEnterEvent *event)
00152 {
00153     // do nothing
00154     KMainWindow::dragEnterEvent(event);
00155 
00156     // accept uri drops only
00157 //    event->accept(QUriDrag::canDecode(event));
00158 }
00159 
00160 void Kandy::dropEvent(QDropEvent *event)
00161 {
00162     // this is a very simplistic implementation of a drop event.  we
00163     // will only accept a dropped URL.  the Qt dnd code can do *much*
00164     // much more, so please read the docs there
00165 
00166     // do nothing
00167     KMainWindow::dropEvent(event);
00168 /*
00169     QStrList uri;
00170 
00171     // see if we can decode a URI.. if not, just ignore it
00172     if (QUriDrag::decode(event, uri))
00173     {
00174         // okay, we have a URI.. process it
00175         QString url, target;
00176         url = uri.first();
00177 
00178         // load in the file
00179         load(url);
00180     }
00181 */
00182 }
00183 
00184 void Kandy::fileOpen()
00185 {
00186     // this slot is called whenever the File->Open menu is selected,
00187     // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
00188     // button is clicked
00189     QString filename = KFileDialog::getOpenFileName();
00190     if (!filename.isEmpty()) load(filename);
00191 }
00192 
00193 void Kandy::fileSave()
00194 {
00195   if (mFilename.isEmpty()) fileSaveAs();
00196   else save(mFilename);
00197 }
00198 
00199 void Kandy::fileSaveAs()
00200 {
00201   QString filename = KFileDialog::getSaveFileName();
00202   save(filename);
00203 }
00204 
00205 void Kandy::filePrint()
00206 {
00207     // this slot is called whenever the File->Print menu is selected,
00208     // the Print shortcut is pressed (usually CTRL+P) or the Print toolbar
00209     // button is clicked
00210     if (!mPrinter) mPrinter = new QPrinter;
00211     if (QPrintDialog::getPrinterSetup(mPrinter))
00212     {
00213         // setup the printer.  with Qt, you always "print" to a
00214         // QPainter.. whether the output medium is a pixmap, a screen,
00215         // or paper
00216         QPainter p;
00217         p.begin(mPrinter);
00218 
00219         // we let our view do the actual printing
00220         QPaintDeviceMetrics metrics(mPrinter);
00221         mView->print(&p, metrics.height(), metrics.width());
00222 
00223         // and send the result to the printer
00224         p.end();
00225     }
00226 }
00227 
00228 void Kandy::optionsShowToolbar()
00229 {
00230     // this is all very cut and paste code for showing/hiding the
00231     // toolbar
00232     if (mToolbarAction->isChecked())
00233         toolBar()->show();
00234     else
00235         toolBar()->hide();
00236 }
00237 
00238 void Kandy::optionsShowStatusbar()
00239 {
00240     // this is all very cut and paste code for showing/hiding the
00241     // statusbar
00242     if (mStatusbarAction->isChecked())
00243         statusBar()->show();
00244     else
00245         statusBar()->hide();
00246 }
00247 
00248 void Kandy::optionsConfigureKeys()
00249 {
00250     KKeyDialog::configureKeys(actionCollection(), "kandyui.rc");
00251 }
00252 
00253 void Kandy::optionsConfigureToolbars()
00254 {
00255     // use the standard toolbar editor
00256     KEditToolbar dlg(actionCollection());
00257     if (dlg.exec())
00258     {
00259         // recreate our GUI
00260         createGUI();
00261     }
00262 }
00263 
00264 void Kandy::optionsPreferences()
00265 {
00266   if (!mPreferencesDialog) {
00267     mPreferencesDialog = new KandyPrefsDialog(this);
00268     mPreferencesDialog->readConfig();
00269   }
00270   
00271   mPreferencesDialog->show();
00272   mPreferencesDialog->raise();
00273 }
00274 
00275 void Kandy::changeStatusbar(const QString& text)
00276 {
00277     // display the text on the statusbar
00278     statusBar()->message(text);
00279 }
00280 
00281 void Kandy::changeCaption(const QString& text)
00282 {
00283     // display the text on the caption
00284     setCaption(text);
00285 }
00286 
00287 void Kandy::setTitle()
00288 {
00289   if (mFilename.isEmpty()) {
00290     setCaption(i18n("New Profile"),mView->isModified());
00291   } else {
00292     setCaption(mFilename,mView->isModified());
00293   }
00294 }
00295 
00296 bool Kandy::queryClose()
00297 {
00298   if (mView->isModified()) {
00299     switch (KMessageBox::warningYesNoCancel(this,
00300         i18n("Save changes to profile %1?").arg(mFilename))) {
00301       case KMessageBox::Yes :
00302         fileSave();
00303         return true;
00304       case KMessageBox::No :
00305         return true;
00306       default: // cancel
00307         return false;
00308     }
00309   } else {
00310     return true;
00311   }
00312 }
00313 
00314 void Kandy::modemConnect()
00315 {
00316   if (!mScheduler->modem()->open()) {
00317     KMessageBox::sorry(this,
00318         i18n("Cannot open modem device %1.")
00319         .arg(KandyPrefs::instance()->mSerialDevice), i18n("Modem Error"));
00320     return;
00321   }
00322   
00323   statusBar()->changeItem(i18n(" Connected "),0);
00324   
00325   emit connectStateChanged(true);
00326 }
00327 
00328 void Kandy::modemDisconnect()
00329 {
00330   mScheduler->modem()->close();
00331 
00332   statusBar()->changeItem(i18n(" Disconnected "),0);
00333 
00334   emit connectStateChanged(false);
00335 }
00336 
00337 void Kandy::showMobileGui()
00338 {
00339   emit showMobileWin();
00340 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:32 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001