diff --git a/glabels/CMakeLists.txt b/glabels/CMakeLists.txt index 2ee7783..109c56b 100644 --- a/glabels/CMakeLists.txt +++ b/glabels/CMakeLists.txt @@ -46,6 +46,7 @@ set (glabels_qobject_headers FieldButton.h FieldMenu.h FieldMenuItem.h + File.h LabelModel.h LabelModelObject.h LabelModelBoxObject.h diff --git a/glabels/File.cpp b/glabels/File.cpp index a5b96eb..de29176 100644 --- a/glabels/File.cpp +++ b/glabels/File.cpp @@ -20,20 +20,147 @@ #include "File.h" +#include "MainWindow.h" +#include "LabelModel.h" #include "NewLabelDialog.h" +#include +#include +#include namespace glabels { /// - /// Open a New Label Dialog + /// New Label Dialog /// - void File::newLabel( QWidget *parent ) + void File::newLabel( MainWindow *window ) { - NewLabelDialog newDialog( parent ); + NewLabelDialog newDialog( window ); newDialog.exec(); } + + /// + /// Open File Dialog + /// + void File::open( MainWindow *window ) + { + QString fileName = + QFileDialog::getOpenFileName( window, + tr("Open label"), + ".", + tr("glabels files (*.glabels);;All files (*)") + ); + if ( !fileName.isEmpty() ) + { + std::cout << "ACTION: file->Open: " << fileName.toStdString() << std::endl; + } + } + + + /// + /// Save file + /// + bool File::save( MainWindow *window ) + { + if ( window->model()->filename().isEmpty() ) + { + return saveAs( window ); + } + else + { + std::cout << "ACTION: file->Save: " << window->model()->filename().toStdString() << std::endl; + return true; + } + } + + + /// + /// Save file as + /// + bool File::saveAs( MainWindow *window ) + { + QString fileName = + QFileDialog::getSaveFileName( window, + tr("Save label"), + ".", + tr("glabels files (*.glabels)") + ); + if ( !fileName.isEmpty() ) + { + std::cout << "ACTION: file->SaveAs: " << fileName.toStdString() << std::endl; + return true; + } + + return false; + } + + + /// + /// Print file + /// + void File::print( MainWindow *window ) + { + std::cout << "ACTION: file->print" << std::endl; + } + + + /// + /// Close file + /// + void File::close( MainWindow *window ) + { + bool closeFlag = true; + + if ( !window->isEmpty() ) + { + QMessageBox msgBox; + msgBox.setText( tr("The document ") + window->model()->shortName() + tr(" has been modified.") ); + msgBox.setInformativeText( tr("Do you want to save your changes?") ); + msgBox.setStandardButtons( QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ); + msgBox.setDefaultButton( QMessageBox::Save ); + + int ret = msgBox.exec(); + + switch (ret) { + case QMessageBox::Save: + // Save was clicked + closeFlag = save( window ); + break; + case QMessageBox::Discard: + // Don't Save was clicked + closeFlag = true; + break; + case QMessageBox::Cancel: + // Cancel was clicked + closeFlag = false; + break; + default: + // should never be reached + closeFlag = false; + break; + } + } + + if ( closeFlag ) + { + window->close(); + } + } + + + /// + /// Exit, closing all windows + /// + void File::exit() + { + foreach ( MainWindow* window, MainWindow::windowList() ) + { + close( window ); + } + } + + } diff --git a/glabels/File.h b/glabels/File.h index fe4e86b..64f7caf 100644 --- a/glabels/File.h +++ b/glabels/File.h @@ -22,19 +22,29 @@ #define glabels_File_h -#include +#include namespace glabels { + class MainWindow; /// /// File Actions /// - namespace File + class File : public QObject { - void newLabel( QWidget *parent ); - } + Q_OBJECT + + public: + static void newLabel( MainWindow *window ); + static void open( MainWindow *window ); + static bool save( MainWindow *window ); + static bool saveAs( MainWindow *window ); + static void print( MainWindow *window ); + static void close( MainWindow *window ); + static void exit(); + }; } diff --git a/glabels/LabelModel.cpp b/glabels/LabelModel.cpp index d01f565..5788200 100644 --- a/glabels/LabelModel.cpp +++ b/glabels/LabelModel.cpp @@ -20,6 +20,7 @@ #include "LabelModel.h" +#include #include #include @@ -33,11 +34,37 @@ namespace glabels /// /// Default constructor. /// - LabelModel::LabelModel() : mModified(true), mTmplate(0), mRotate(false) + LabelModel::LabelModel() : mUntitledInstance(0), mModified(true), mTmplate(0), mRotate(false) { } + /// + /// Short name. + /// + QString LabelModel::shortName() + { + static int untitledCount = 0; + + if ( mFilename.isEmpty() ) + { + if ( mUntitledInstance == 0 ) + { + mUntitledInstance = ++untitledCount; + } + QString numString; + numString.setNum(mUntitledInstance);; + + return tr("Untitled") + numString; + } + else + { + QFileInfo fileInfo( mFilename ); + return fileInfo.baseName(); + } + } + + /// /// Add object. /// diff --git a/glabels/LabelModel.h b/glabels/LabelModel.h index 4cb5c0d..fb9824c 100644 --- a/glabels/LabelModel.h +++ b/glabels/LabelModel.h @@ -76,6 +76,7 @@ namespace glabels inline bool isModified() const; inline void clearModified(); + QString shortName(); inline const QString& filename() const; inline void setFilename( const QString &filename ); @@ -180,6 +181,7 @@ namespace glabels // Private data ///////////////////////////////// private: + int mUntitledInstance; bool mModified; QString mFilename; int mCompressionLevel; diff --git a/glabels/MainWindow.cpp b/glabels/MainWindow.cpp index 097e488..e3eb8a5 100644 --- a/glabels/MainWindow.cpp +++ b/glabels/MainWindow.cpp @@ -43,6 +43,12 @@ namespace glabels { + /// + /// Static window list + /// + QList MainWindow::smWindowList; + + /// /// Constructor /// @@ -53,9 +59,9 @@ namespace glabels QLabel* tmp = new QLabel( "Coming Soon..." ); setCentralWidget( tmp ); #else - LabelModel* model = new LabelModel(); + mModel = new LabelModel(); const libglabels::Template* tmplate = libglabels::Db::lookupTemplateFromName( "Avery 5163" ); - model->setTmplate( tmplate ); + mModel->setTmplate( tmplate ); LabelModelBoxObject* object = new LabelModelBoxObject(); object->setW( 36 ); object->setH( 36 ); @@ -69,12 +75,12 @@ namespace glabels object->setShadowX( 5 ); object->setShadowY( 5 ); object->setShadow( true ); - model->addObject( object ); + mModel->addObject( object ); - view = new View(); - view->setModel( model ); + mView = new View(); + mView->setModel( mModel ); - setCentralWidget( view ); + setCentralWidget( mView ); #endif ///////////////////////////////////////////////// @@ -87,6 +93,43 @@ namespace glabels setPasteVerbsEnabled( false ); readSettings(); + + smWindowList.push_back( this ); + } + + + /// + /// Destructor + /// + MainWindow::~MainWindow() + { + smWindowList.removeOne( this ); + } + + + /// + /// Get model accessor + /// + LabelModel* MainWindow::model() const + { + return mModel; + } + + + /// + /// Is window empty? + /// + bool MainWindow::isEmpty() const + { + return mModel == 0; + } + + /// + /// Get window list + /// + QList MainWindow::windowList() + { + return smWindowList; } @@ -537,9 +580,9 @@ namespace glabels updateZoomInfo(); updateCursorInfo(); - connect( view, SIGNAL(zoomChanged()), this, SLOT(updateZoomInfo()) ); - connect( view, SIGNAL(pointerMoved(double, double)), this, SLOT(updateCursorInfo(double, double)) ); - connect( view, SIGNAL(pointerExited()), this, SLOT(updateCursorInfo()) ); + connect( mView, SIGNAL(zoomChanged()), this, SLOT(updateZoomInfo()) ); + connect( mView, SIGNAL(pointerMoved(double, double)), this, SLOT(updateCursorInfo(double, double)) ); + connect( mView, SIGNAL(pointerExited()), this, SLOT(updateCursorInfo()) ); } @@ -680,8 +723,8 @@ namespace glabels objectsToolBar->setVisible( showObjectsToolBar ); editToolBar ->setVisible( showEditToolBar ); viewToolBar ->setVisible( showViewToolBar ); - view ->setGridVisible( showGrid ); - view ->setMarkupVisible( showMarkup ); + mView ->setGridVisible( showGrid ); + mView ->setMarkupVisible( showMarkup ); } @@ -717,7 +760,7 @@ namespace glabels /// void MainWindow::fileOpen() { - std::cout << "ACTION: file->Open" << std::endl; + File::open( this ); } @@ -726,7 +769,7 @@ namespace glabels /// void MainWindow::fileSave() { - std::cout << "ACTION: file->Save" << std::endl; + File::save( this ); } @@ -735,7 +778,7 @@ namespace glabels /// void MainWindow::fileSaveAs() { - std::cout << "ACTION: file->Save As" << std::endl; + File::saveAs( this ); } @@ -744,7 +787,7 @@ namespace glabels /// void MainWindow::filePrint() { - std::cout << "ACTION: file->Print" << std::endl; + File::print( this ); } @@ -771,7 +814,7 @@ namespace glabels /// void MainWindow::fileClose() { - std::cout << "ACTION: file->Close" << std::endl; + File::close( this ); } @@ -780,7 +823,7 @@ namespace glabels /// void MainWindow::fileExit() { - std::cout << "ACTION: file->Exit" << std::endl; + File::exit(); } @@ -906,7 +949,7 @@ namespace glabels /// void MainWindow::viewGrid( bool state ) { - view->setGridVisible( state ); + mView->setGridVisible( state ); } @@ -915,7 +958,7 @@ namespace glabels /// void MainWindow::viewMarkup( bool state ) { - view->setMarkupVisible( state ); + mView->setMarkupVisible( state ); } @@ -924,7 +967,7 @@ namespace glabels /// void MainWindow::viewZoomIn() { - view->zoomIn(); + mView->zoomIn(); } @@ -933,7 +976,7 @@ namespace glabels /// void MainWindow::viewZoomOut() { - view->zoomOut(); + mView->zoomOut(); } @@ -942,7 +985,7 @@ namespace glabels /// void MainWindow::viewZoom1To1() { - view->zoom1To1(); + mView->zoom1To1(); } @@ -951,7 +994,7 @@ namespace glabels /// void MainWindow::viewZoomToFit() { - view->zoomToFit(); + mView->zoomToFit(); } @@ -1176,10 +1219,10 @@ namespace glabels /// void MainWindow::updateZoomInfo() { - zoomInfoLabel->setText( QString( " %1% " ).arg(100*view->zoom(), 0, 'f', 0) ); + zoomInfoLabel->setText( QString( " %1% " ).arg(100*mView->zoom(), 0, 'f', 0) ); - viewZoomInAction->setEnabled( !view->isZoomMax() ); - viewZoomOutAction->setEnabled( !view->isZoomMin() ); + viewZoomInAction->setEnabled( !mView->isZoomMax() ); + viewZoomOutAction->setEnabled( !mView->isZoomMin() ); } diff --git a/glabels/MainWindow.h b/glabels/MainWindow.h index 36b77a7..db25ea9 100644 --- a/glabels/MainWindow.h +++ b/glabels/MainWindow.h @@ -34,6 +34,7 @@ class QLabel; namespace glabels { // Forward References + class LabelModel; class View; @@ -50,6 +51,17 @@ namespace glabels ///////////////////////////////////// public: MainWindow(); + virtual ~MainWindow(); + + + ///////////////////////////////////// + // Public Methods + ///////////////////////////////////// + public: + LabelModel* model() const; + bool isEmpty() const; + + static QList windowList(); ///////////////////////////////////// @@ -148,6 +160,8 @@ namespace glabels // Private Data ///////////////////////////////////// private: + static QList smWindowList; + QMenu* fileMenu; QMenu* editMenu; QMenu* viewMenu; @@ -165,7 +179,8 @@ namespace glabels QToolBar* editToolBar; QToolBar* viewToolBar; - View* view; + LabelModel* mModel; + View* mView; QLabel* zoomInfoLabel; QLabel* cursorInfoLabel;