Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tinykate.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                              tinykate.cpp
00003                         Tiny KATE mainwindow
00004                              -------------------
00005     begin                : November 2002
00006     copyright            : (C) 2002 by Joseph Wenninger <jowenn@kde.org>
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free softwaSre; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation.                                         *
00014  *   ONLY VERSION 2 OF THE LICENSE IS APPLICABLE                           *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "tinykate.h"
00019 
00020 #include "katedocument.h"
00021 #include "kglobal.h"
00022 
00023 /* OPIE */
00024 #include <opie2/odebug.h>
00025 #include <opie2/ofiledialog.h>
00026 #include <opie2/oresource.h>
00027 #include <qpe/qpeapplication.h>
00028 
00029 /* QT */
00030 #include <qaction.h>
00031 #include <qtoolbutton.h>
00032 #include <qmenubar.h>
00033 #include <qmessagebox.h>
00034 
00035 
00036 using namespace Opie::Ui;
00037 TinyKate::TinyKate( QWidget *parent, const char *name, WFlags f) :
00038         QMainWindow( parent, name, f )
00039 {
00040     shutDown=false;
00041     nextUnnamed=0;
00042     currentView=0;
00043     viewCount=0;
00044     setCaption(tr("TinyKATE"));
00045     KGlobal::setAppName("TinyKATE");
00046 
00047     QMenuBar *mb = new QMenuBar( this );
00048     mb->setMargin( 0 );
00049 
00050     tabwidget=new OTabWidget(this);
00051     setCentralWidget(tabwidget);
00052     connect(tabwidget,SIGNAL(currentChanged(QWidget*)),this,SLOT(slotCurrentChanged(QWidget*)));
00053 
00054     //FILE ACTIONS
00055     QPopupMenu *popup = new QPopupMenu( this );
00056 
00057     // Action for creating a new document
00058     QAction *a = new QAction( tr( "New" ), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
00059                               QString::null, 0, this, 0 );
00060     a->addTo( popup );
00061     connect(a, SIGNAL(activated()), this, SLOT(slotNew()));
00062 
00063     // Action for opening an exisiting document
00064     a = new QAction( tr( "Open" ), Opie::Core::OResource::loadPixmap( "fileopen", Opie::Core::OResource::SmallIcon ),
00065                      QString::null, 0, this, 0 );
00066     a->addTo(popup);
00067     connect(a, SIGNAL(activated()), this, SLOT(slotOpen()));
00068 
00069 
00070     // Action for saving  document
00071     a = new QAction( tr( "Save" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
00072                      QString::null, 0, this, 0 );
00073     a->addTo(popup);
00074     connect(a, SIGNAL(activated()), this, SLOT(slotSave()));
00075 
00076     // Action for saving document to a new name
00077     a = new QAction( tr( "Save As" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
00078                      QString::null, 0, this, 0 );
00079     a->addTo(popup);
00080     connect(a, SIGNAL(activated()), this, SLOT(slotSaveAs()));
00081 
00082     // Action for closing the currently active document
00083     a = new QAction( tr( "Close" ), Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon ),
00084                      QString::null, 0, this, 0 );
00085     a->addTo(popup);
00086     connect(a, SIGNAL(activated()), this, SLOT(slotClose()));
00087 
00088 
00089     mb->insertItem(tr("File"),popup);
00090 
00091     //EDIT ACTIONS
00092     bool useBigIcon = qApp->desktop()->size().width() > 330;
00093 
00094     // Action for cutting text
00095     editCut = new QToolButton( 0 );
00096     editCut->setUsesBigPixmap( useBigIcon );
00097     editCut->setAutoRaise( true );
00098     editCut->setIconSet( Opie::Core::OResource::loadPixmap( "cut", Opie::Core::OResource::SmallIcon ) );
00099 
00100     // Action for Copying text
00101     editCopy = new QToolButton( 0 );
00102     editCopy->setUsesBigPixmap( useBigIcon );
00103     editCopy->setAutoRaise( true );
00104     editCopy->setIconSet( Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon ) );
00105 
00106     // Action for pasting text
00107     editPaste =  new QToolButton( 0 );
00108     editPaste->setUsesBigPixmap( useBigIcon );
00109     editPaste->setAutoRaise( true );
00110     editPaste->setIconSet( Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon ) );
00111 
00112     // Action for finding / replacing  text
00113     editFindReplace = new QToolButton( 0 );
00114     editFindReplace->setUsesBigPixmap( useBigIcon );
00115     editFindReplace->setAutoRaise( true );
00116     editFindReplace->setIconSet( Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ) );
00117 
00118     // Action for undo
00119     editUndo = new QToolButton( 0 );
00120     editUndo->setUsesBigPixmap( useBigIcon );
00121     editUndo->setAutoRaise( true );
00122     editUndo->setIconSet( Opie::Core::OResource::loadPixmap( "undo", Opie::Core::OResource::SmallIcon ) );
00123 
00124     // Action for redo
00125     editRedo = new QToolButton( 0 );
00126     editRedo->setUsesBigPixmap( useBigIcon );
00127     editRedo->setAutoRaise( true );
00128     editRedo->setIconSet( Opie::Core::OResource::loadPixmap( "redo", Opie::Core::OResource::SmallIcon ) );
00129 
00130     //VIEW ACITONS
00131     popup = new QPopupMenu( this );
00132 
00133     viewIncFontSizes = new QAction( tr( "Font +" ), QString::null, 0, this, 0 );
00134     viewIncFontSizes->addTo( popup );
00135 
00136     viewDecFontSizes = new QAction( tr( "Font -" ), QString::null, 0, this, 0 );
00137     viewDecFontSizes->addTo( popup );
00138 
00139     mb->insertItem(tr("View"),popup);
00140 
00141     popup = new QPopupMenu( this );
00142     mb->insertItem(tr("Utils"),popup);
00143 
00144 
00145     mb->insertItem( editCut );
00146     mb->insertItem( editCopy );
00147     mb->insertItem( editPaste );
00148     mb->insertItem( editFindReplace );
00149     mb->insertItem( editUndo );
00150     mb->insertItem(  editRedo );
00151 
00152 
00153     //Highlight management
00154     hlmenu=new QPopupMenu(this);
00155     HlManager *hlm=HlManager::self();
00156     for (int i=0;i<hlm->highlights();i++)
00157     {
00158         hlmenu->insertItem(hlm->hlName(i),i);
00159     }
00160     popup->insertItem(tr("Highlighting"),hlmenu);
00161 
00162 
00163     utilSettings = new QAction( tr( "Settings" ),
00164                                 Opie::Core::OResource::loadPixmap( "SettingsIcon", Opie::Core::OResource::SmallIcon ),
00165                                 QString::null, 0, this, 0 );
00166     utilSettings->addTo( popup);
00167 
00168     if( qApp->argc() > 1) open(qApp->argv()[1]);
00169     else slotNew();
00170 
00171 }
00172 
00173 TinyKate::~TinyKate( )
00174 {
00175     owarn << "TinyKate destructor\n" << oendl;
00176 
00177     shutDown=true;
00178     while (currentView!=0)
00179     {
00180         slotClose();
00181     }
00182 
00183     if( KGlobal::config() != 0 )
00184     {
00185         owarn << "deleting KateConfig object..\n" << oendl;
00186         delete KGlobal::config();
00187     }
00188 }
00189 
00190 void TinyKate::slotOpen( )
00191 {
00192     QString filename = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
00193                        QString::null);
00194     if (!filename.isEmpty())
00195     {
00196         open(filename);
00197     }
00198 }
00199 
00200 void TinyKate::open(const QString & filename)
00201 {
00202     KateDocument *kd= new KateDocument(false, false, this,0,this);
00203     KTextEditor::View *kv;
00204     QString realFileName;
00205     //check if filename is a .desktop file
00206     if ( filename.find( ".desktop", 0, true ) != -1 ) {
00207         switch ( QMessageBox::warning( this, tr( "TinyKATE" ),
00208                                        tr("TinyKATE has detected<BR>you selected a <B>.desktop</B> file.<BR>Open <B>.desktop</B> file or <B>linked</B> file?" ),
00209                                        tr(".desktop File"),
00210                                        tr("Linked Document"), 0, 1, 1 ) )
00211         {
00212         case 0: //desktop
00213             realFileName = filename;
00214             break;
00215         case 1: //linked
00216             DocLnk docLnk( filename );
00217             realFileName = docLnk.file();
00218             break;
00219         };
00220     } else {
00221             realFileName = filename;
00222     }
00223 
00224     QFileInfo fileInfo( realFileName );
00225     QString filenamed = fileInfo.fileName();
00226     tabwidget->addTab(kv=kd->createView(tabwidget,"bLAH"),"tinykate/tinykate", filenamed );
00227     odebug << realFileName << oendl;
00228 
00229     kd->setDocName( filenamed);
00230     kd->open( realFileName );
00231     viewCount++;
00232 }
00233 
00234 void TinyKate::setDocument(const QString& fileref)
00235 {
00236     open( fileref );
00237 }
00238 
00239 void TinyKate::slotCurrentChanged( QWidget * view)
00240 {
00241     if (currentView)
00242     {
00243 
00244         disconnect(editCopy,SIGNAL(clicked()),currentView,SLOT(copy()));
00245         disconnect(editCut,SIGNAL(clicked()),currentView,SLOT(cut()));
00246         disconnect(editPaste,SIGNAL(clicked()),currentView,SLOT(paste()));
00247         disconnect(editUndo,SIGNAL(clicked()),currentView,SLOT(undo()));
00248         disconnect(editRedo,SIGNAL(clicked()),currentView,SLOT(redo()));
00249         disconnect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
00250         disconnect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
00251         disconnect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
00252         disconnect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));
00253     }
00254 
00255     currentView=(KTextEditor::View*)view;
00256 
00257     connect(editCopy,SIGNAL(clicked()),currentView,SLOT(copy()));
00258     connect(editCut,SIGNAL(clicked()),currentView,SLOT(cut()));
00259     connect(editPaste,SIGNAL(clicked()),currentView,SLOT(paste()));
00260     connect(editUndo,SIGNAL(clicked()),currentView,SLOT(undo()));
00261     connect(editRedo,SIGNAL(clicked()),currentView,SLOT(redo()));
00262     connect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
00263     connect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
00264     connect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
00265     connect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));
00266 
00267 }
00268 
00269 void TinyKate::slotNew( )
00270 {
00271     KateDocument *kd= new KateDocument(false, false, this,0,this);
00272     KTextEditor::View *kv;
00273     tabwidget->addTab(kv=kd->createView(tabwidget,"BLAH"),
00274                       "tinykate/tinykate",
00275                       tr("Unnamed %1").arg(nextUnnamed++));
00276     viewCount++;
00277 }
00278 
00279 void TinyKate::slotClose( )
00280 {
00281     if (currentView==0) return;
00282     KTextEditor::View *dv=currentView;
00283     currentView=0;
00284     tabwidget->removePage(dv);
00285     delete dv->document();
00286     viewCount--;
00287     if ((!viewCount) && (!shutDown))  slotNew();
00288 }
00289 
00290 void TinyKate::slotSave()
00291 {
00292     // feel free to make this how you want
00293     if (currentView==0) return;
00294 
00295     //  KateView *kv = (KateView*) currentView;
00296     KateDocument *kd = (KateDocument*) currentView->document();
00297     //  odebug << "saving file "+kd->docName() << oendl;
00298     if( kd->docName().isEmpty())
00299         slotSaveAs();
00300     else
00301         kd->saveFile();
00302     //    kv->save();
00303     //    kd->saveFile();
00304 }
00305 
00306 void TinyKate::slotSaveAs()
00307 {
00308     if (currentView==0) return;
00309     KateDocument *kd = (KateDocument*) currentView->document();
00310 
00311     QString filename= OFileDialog::getSaveFileName(OFileSelector::EXTENDED_ALL,
00312                       QString::null);
00313     if (!filename.isEmpty())
00314     {
00315         odebug << "saving file "+filename << oendl;
00316         QFileInfo fi(filename);
00317         QString filenamed = fi.fileName();
00318         kd->setDocFile( filename);
00319         kd->setDocName( filenamed);
00320         kd->saveFile();
00321         //   KTextEditor::View *dv = currentView;
00322         //     tabwidget->changeTab( dv, filenamed);
00323         // need to change tab label here
00324     }
00325 
00326 }

Generated on Sat Nov 5 16:17:14 2005 for OPIE by  doxygen 1.4.2