/**************************************************************************** ** $Id: application.cpp,v 1.1 2004/09/18 17:31:23 phil Exp $ ** ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include "application.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "filesave.xpm" #include "fileopen.xpm" #include "fileprint.xpm" ApplicationWindow::ApplicationWindow() : QMainWindow( 0, "example application main window", WDestructiveClose | WGroupLeader ) { printer = new QextScintillaPrinter( QPrinter::HighResolution ); QPixmap openIcon, saveIcon, printIcon; QToolBar * fileTools = new QToolBar( this, "file operations" ); fileTools->setLabel( "File Operations" ); openIcon = QPixmap( fileopen ); QToolButton * fileOpen = new QToolButton( openIcon, "Open File", QString::null, this, SLOT(choose()), fileTools, "open file" ); saveIcon = QPixmap( filesave ); QToolButton * fileSave = new QToolButton( saveIcon, "Save File", QString::null, this, SLOT(save()), fileTools, "save file" ); printIcon = QPixmap( fileprint ); QToolButton * filePrint = new QToolButton( printIcon, "Print File", QString::null, this, SLOT(print()), fileTools, "print file" ); (void)QWhatsThis::whatsThisButton( fileTools ); const char * fileOpenText = "

" "Click this button to open a new file.
" "You can also select the Open command " "from the File menu.

"; QWhatsThis::add( fileOpen, fileOpenText ); QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon ); const char * fileSaveText = "

Click this button to save the file you " "are editing. You will be prompted for a file name.\n" "You can also select the Save command " "from the File menu.

"; QWhatsThis::add( fileSave, fileSaveText ); const char * filePrintText = "Click this button to print the file you " "are editing.\n" "You can also select the Print command " "from the File menu."; QWhatsThis::add( filePrint, filePrintText ); QPopupMenu * file = new QPopupMenu( this ); menuBar()->insertItem( "&File", file ); file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N ); int id; id = file->insertItem( openIcon, "&Open...", this, SLOT(choose()), CTRL+Key_O ); file->setWhatsThis( id, fileOpenText ); id = file->insertItem( saveIcon, "&Save", this, SLOT(save()), CTRL+Key_S ); file->setWhatsThis( id, fileSaveText ); id = file->insertItem( "Save &As...", this, SLOT(saveAs()) ); file->setWhatsThis( id, fileSaveText ); file->insertSeparator(); id = file->insertItem( printIcon, "&Print...", this, SLOT(print()), CTRL+Key_P ); file->setWhatsThis( id, filePrintText ); file->insertSeparator(); file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W ); file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q ); menuBar()->insertSeparator(); QPopupMenu * help = new QPopupMenu( this ); menuBar()->insertItem( "&Help", help ); help->insertItem( "&About", this, SLOT(about()), Key_F1 ); help->insertItem( "About &Qt", this, SLOT(aboutQt()) ); help->insertSeparator(); help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 ); e = new QextScintilla( this, "editor" ); e->setFocus(); setCentralWidget( e ); statusBar()->message( "Ready", 2000 ); resize( 450, 600 ); } ApplicationWindow::~ApplicationWindow() { delete printer; } void ApplicationWindow::newDoc() { ApplicationWindow *ed = new ApplicationWindow; ed->setCaption("QScintilla Example - Application"); ed->show(); } void ApplicationWindow::choose() { QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this); if ( !fn.isEmpty() ) load( fn ); else statusBar()->message( "Loading aborted", 2000 ); } void ApplicationWindow::load( const QString &fileName ) { QFile f( fileName ); if ( !f.open( IO_ReadOnly ) ) return; QTextStream ts( &f ); e->setText( ts.read() ); e->setModified( FALSE ); setCaption( fileName ); statusBar()->message( "Loaded document " + fileName, 2000 ); } void ApplicationWindow::save() { if ( filename.isEmpty() ) { saveAs(); return; } QString text = e->text(); QFile f( filename ); if ( !f.open( IO_WriteOnly ) ) { statusBar()->message( QString("Could not write to %1").arg(filename), 2000 ); return; } QTextStream t( &f ); t << text; f.close(); e->setModified( FALSE ); setCaption( filename ); statusBar()->message( QString( "File %1 saved" ).arg( filename ), 2000 ); } void ApplicationWindow::saveAs() { QString fn = QFileDialog::getSaveFileName( QString::null, QString::null, this ); if ( !fn.isEmpty() ) { filename = fn; save(); } else { statusBar()->message( "Saving aborted", 2000 ); } } void ApplicationWindow::print() { printer->setFullPage( TRUE ); if ( printer->setup(this) ) { // printer dialog statusBar()->message( "Printing..." ); if (printer->printRange( e )) statusBar()->message( "Printing completed", 2000 ); else statusBar()->message( "Error while printing", 2000 ); } else { statusBar()->message( "Printing aborted", 2000 ); } } void ApplicationWindow::closeEvent( QCloseEvent* ce ) { if ( !e->isModified() ) { ce->accept(); return; } switch( QMessageBox::information( this, "QScintilla Application Example", "Do you want to save the changes" " to the document?", "Yes", "No", "Cancel", 0, 1 ) ) { case 0: save(); ce->accept(); break; case 1: ce->accept(); break; case 2: default: // just for sanity ce->ignore(); break; } } void ApplicationWindow::about() { QMessageBox::about( this, "QScintilla Application Example", "This example demonstrates a simple use of " "QextScintilla and QextScintillaPrinter."); } void ApplicationWindow::aboutQt() { QMessageBox::aboutQt( this, "QScintilla Application Example" ); }