kandy

kandyview.cpp

00001 /*
00002     This file is part of Kandy.
00003 
00004     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <unistd.h>
00026 
00027 #include <qpainter.h>
00028 #include <qlayout.h>
00029 #include <qhbox.h>
00030 #include <qvbox.h>
00031 #include <qtextedit.h>
00032 #include <qlistview.h>
00033 #include <qdom.h>
00034 #include <qtextstream.h>
00035 #include <qfile.h>
00036 #include <qlineedit.h>
00037 #include <qcheckbox.h>
00038 #include <qlabel.h>
00039 #include <qpushbutton.h>
00040 
00041 #include <kurl.h>
00042 #include <kmessagebox.h>
00043 #include <kdebug.h>
00044 #include <klocale.h>
00045 #include <kglobal.h>
00046 #include <kconfig.h>
00047 #include <kinputdialog.h>
00048 #include <kdialog.h>
00049 
00050 #include "modem.h"
00051 #include "cmdpropertiesdialog.h"
00052 #include "commanditem.h"
00053 #include "atcommand.h"
00054 #include "commandscheduler.h"
00055 #include "kandyprefs.h"
00056 
00057 #include "kandyview.h"
00058 #include "kandyview.moc"
00059 
00060 KandyView::KandyView(CommandScheduler *scheduler,QWidget *parent)
00061     : QWidget(parent)
00062 {
00063   mModified = false;
00064   mScheduler = scheduler;
00065 
00066   QBoxLayout *topLayout = new QVBoxLayout( this );
00067 
00068   QSplitter *mainSplitter = new QSplitter( Horizontal, this );
00069   topLayout->addWidget( mainSplitter );
00070 
00071   QWidget *commandBox = new QWidget( mainSplitter );
00072 
00073   QBoxLayout *commandLayout = new QVBoxLayout( commandBox );
00074   commandLayout->setMargin( KDialog::marginHint() );
00075   commandLayout->setSpacing( KDialog::spacingHint() );
00076 
00077   mCommandList = new QListView( commandBox );
00078   mCommandList->addColumn( i18n( "Name" ) );
00079   mCommandList->addColumn( i18n( "Command" ) );
00080   mCommandList->addColumn( i18n( "Hex" ) );
00081   commandLayout->addWidget( mCommandList );
00082 
00083   connect( mCommandList, SIGNAL( doubleClicked(QListViewItem*) ),
00084            SLOT( executeCommand() ) );
00085 
00086   QPushButton *buttonAdd = new QPushButton( i18n("Add..."), commandBox );
00087   commandLayout->addWidget( buttonAdd );
00088   connect( buttonAdd, SIGNAL( clicked() ), SLOT( addCommand() ) );
00089 
00090   QPushButton *buttonEdit = new QPushButton( i18n("Edit..."), commandBox );
00091   commandLayout->addWidget( buttonEdit );
00092   connect( buttonEdit, SIGNAL( clicked() ), SLOT( editCommand() ) );
00093 
00094   QPushButton *buttonDelete = new QPushButton( i18n("Delete"), commandBox );
00095   commandLayout->addWidget( buttonDelete );
00096   connect( buttonDelete, SIGNAL( clicked() ), SLOT( deleteCommand() ) );
00097 
00098   QPushButton *buttonExecute = new QPushButton( i18n("Execute"), commandBox );
00099   commandLayout->addWidget( buttonExecute );
00100   connect( buttonExecute, SIGNAL( clicked() ), SLOT( executeCommand() ) );
00101 
00102   QSplitter *ioSplitter = new QSplitter( Vertical, mainSplitter );
00103 
00104   QWidget *inBox = new QWidget( ioSplitter );
00105   
00106   QBoxLayout *inLayout = new QVBoxLayout( inBox );
00107 
00108   QLabel *inLabel = new QLabel( i18n("Input:"), inBox );
00109   inLabel->setMargin( 2 );
00110   inLayout->addWidget( inLabel );
00111 
00112   mInput = new QTextEdit( inBox );
00113   inLayout->addWidget( mInput );
00114   
00115   QWidget *outBox = new QWidget( ioSplitter );
00116 
00117   QBoxLayout *outLayout = new QVBoxLayout( outBox );
00118 
00119   QLabel *outLabel = new QLabel( i18n( "Output:"), outBox );
00120   outLabel->setMargin( 2 );
00121   outLayout->addWidget( outLabel );
00122 
00123   mOutput = new QTextEdit( outBox );
00124   mOutput->setReadOnly( true );
00125   outLayout->addWidget( mOutput );
00126 
00127   QVBox *resultBox = new QVBox( mainSplitter );
00128 
00129   QLabel *resultLabel = new QLabel( i18n("Result:"), resultBox );
00130   resultLabel->setMargin( 2 );
00131     
00132   mResultView = new QTextEdit( resultBox );
00133   mResultView->setReadOnly( true );
00134   
00135   connect (mInput,SIGNAL(returnPressed()),SLOT(processLastLine()));
00136 
00137   connect(mScheduler->modem(),SIGNAL(gotLine(const char *)),
00138           SLOT(appendOutput(const char *)));
00139 
00140   connect(mScheduler,SIGNAL(result(const QString &)),
00141           mResultView,SLOT(setText(const QString &)));
00142   connect(mScheduler,SIGNAL(commandProcessed(ATCommand *)),
00143           SLOT(setResult(ATCommand *)));
00144 }
00145 
00146 KandyView::~KandyView()
00147 {
00148 }
00149 
00150 
00151 void KandyView::print(QPainter *, int, int)
00152 {
00153     // do the actual printing, here
00154     // p->drawText(etc..)
00155 }
00156 
00157 void KandyView::importPhonebook()
00158 {
00159 #if 0
00160   createMobileGui();
00161   connect (mMobileGui,SIGNAL(phonebookRead()),mMobileGui,SLOT(writeKab()));
00162   mMobileGui->readPhonebook();
00163 #endif
00164 }
00165 
00166 void KandyView::slotSetTitle(const QString& title)
00167 {
00168     emit signalChangeCaption(title);
00169 }
00170 
00171 void KandyView::processLastLine()
00172 {
00173   int para = 0;
00174   int row = 0;
00175   mInput->getCursorPosition( &para, &row );
00176   
00177   if ( para > 0 ) {
00178     mLastInput = mInput->text( para - 1 );
00179 
00180     kdDebug(5960) << "processLastLine(): " << mLastInput << endl;
00181 
00182     mScheduler->execute(mLastInput);
00183   }
00184 }
00185 
00186 void KandyView::appendOutput(const char *line)
00187 {
00188 //  kdDebug(5960) << "OUT: " << line << endl;
00189   mOutput->append(line);
00190   mOutput->setCursorPosition(mOutput->paragraphs()-1,0);
00191 }
00192 
00193 void KandyView::setResult(ATCommand *command)
00194 {
00195   if (command == 0) {
00196     kdDebug(5960) << "KandyView::setResult(): Error! No command." << endl;
00197     mResultView->setText(i18n("Error"));
00198     return;
00199   }
00200   
00201 //  kdDebug(5960) << "KandyView::setResult(): " << endl << mResult << endl
00202 //            << mLastCommand->processOutput(mResult) << endl;
00203   
00204   mResultView->setText(command->cmdName() + ":\n" + command->processOutput());
00205 }
00206 
00207 void KandyView::addCommand()
00208 {
00209   ATCommand *cmd = new ATCommand(mLastInput);
00210 
00211   CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true);
00212 
00213   int result = dlg->exec();
00214 
00215   if (result == QDialog::Accepted) {
00216     new CommandItem(mCommandList,cmd);
00217     mScheduler->commandSet()->addCommand(cmd);
00218     setModified();
00219   } else {
00220     delete cmd;
00221   }
00222 }
00223 
00224 void KandyView::editCommand()
00225 {
00226   QListViewItem *item = mCommandList->currentItem();
00227   if (item) {
00228     CommandItem *cmdItem = (CommandItem *)item;
00229     ATCommand *cmd = cmdItem->command();
00230 
00231     CmdPropertiesDialog *dlg = new CmdPropertiesDialog(cmd,this,"cmdprop",true);
00232 
00233     int result = dlg->exec();
00234 
00235     if (result == QDialog::Accepted) {
00236       cmdItem->setItemText();
00237       setModified();
00238     }
00239   }
00240 }
00241 
00242 void KandyView::executeCommand()
00243 {
00244   CommandItem *item = (CommandItem *)(mCommandList->currentItem());
00245   if (item) {
00246     ATCommand *cmd = item->command();
00247     QPtrList<ATParameter> paraList = cmd->parameters();
00248     for(uint i=0;i<paraList.count();++i) {
00249       ATParameter *p = paraList.at(i);
00250       if (p->userInput()) {
00251         bool ok = false;
00252         QString value = KInputDialog::getText(QString::null,
00253             i18n("Enter value for %1:").arg(p->name()),QString::null,&ok,this);
00254         if (!ok)
00255           return;
00256         p->setValue(value);
00257       }
00258     }
00259     kdDebug(5960) << "KandyView::executeCommand(): " << cmd->cmd() << endl;
00260     mScheduler->execute(cmd);
00261   }
00262 }
00263 
00264 void KandyView::deleteCommand()
00265 {
00266   CommandItem *item = dynamic_cast<CommandItem *>(mCommandList->currentItem());
00267   if (item) {
00268     mScheduler->commandSet()->deleteCommand(item->command());
00269     delete item;
00270     setModified();
00271   }
00272 }
00273 
00274 bool KandyView::loadFile(const QString& filename)
00275 {
00276   mCommandList->clear();
00277 
00278   if (!mScheduler->loadProfile(filename)) return false;
00279 
00280   QPtrList<ATCommand> *cmds = mScheduler->commandSet()->commandList();
00281 
00282   for(uint i=0;i<cmds->count();++i) {
00283     new CommandItem(mCommandList,cmds->at(i));
00284   }
00285 
00286   KConfig *config = KGlobal::config();
00287   config->setGroup("General");
00288   config->writeEntry("CurrentProfile",filename);
00289 
00290   setModified(false);
00291 
00292   return true;
00293 }
00294 
00295 bool KandyView::saveFile(const QString& filename)
00296 {
00297   if (!mScheduler->saveProfile(filename)) return false;
00298 
00299   KConfig *config = KGlobal::config();
00300   config->setGroup("General");
00301   config->writeEntry("CurrentProfile",filename);
00302 
00303   setModified(false);
00304 
00305   return true;
00306 }
00307 
00308 void KandyView::setModified(bool modified)
00309 {
00310   if (modified != mModified) {
00311     mModified = modified;
00312     emit modifiedChanged(mModified);
00313   }
00314 }
00315 
00316 bool KandyView::isModified()
00317 {
00318   return mModified;
00319 }
KDE Home | KDE Accessibility Home | Description of Access Keys