From 3bb129b1d18b1fd7f9e689dbe65911f748bb0bb5 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Fri, 21 Aug 2015 00:43:45 -0400 Subject: [PATCH] Created skeletal rich print preview widget. --- glabels/CMakeLists.txt | 2 + glabels/MainWindow.cpp | 14 +-- glabels/MainWindow.h | 1 + glabels/Preview.cpp | 175 ++++++++++++++++++++++++++++++++++++++ glabels/Preview.h | 85 ++++++++++++++++++ glabels/PrintView.cpp | 2 +- glabels/SimplePreview.cpp | 2 +- glabels/SimplePreview.h | 2 +- glabels/ui/PrintView.ui | 9 +- 9 files changed, 280 insertions(+), 12 deletions(-) create mode 100644 glabels/Preview.cpp create mode 100644 glabels/Preview.h diff --git a/glabels/CMakeLists.txt b/glabels/CMakeLists.txt index 855606f..02c9ace 100644 --- a/glabels/CMakeLists.txt +++ b/glabels/CMakeLists.txt @@ -35,6 +35,7 @@ set (glabels_sources ObjectEditor.cpp Outline.cpp PrintView.cpp + Preview.cpp TemplatePicker.cpp TemplatePickerItem.cpp TextNode.cpp @@ -64,6 +65,7 @@ set (glabels_qobject_headers NewLabelDialog.h ObjectEditor.h PrintView.h + Preview.h SimplePreview.h TemplatePicker.h View.h diff --git a/glabels/MainWindow.cpp b/glabels/MainWindow.cpp index 9116013..9d4b654 100644 --- a/glabels/MainWindow.cpp +++ b/glabels/MainWindow.cpp @@ -69,12 +69,13 @@ namespace glabels QWidget* mergePage = createMergePage(); QWidget* printPage = createPrintPage(); - QTabWidget* notebook = new QTabWidget(); - notebook->addTab( editorPage, "Editor" ); - notebook->addTab( mergePage, "Merge" ); - notebook->addTab( printPage, "Print" ); - - setCentralWidget( notebook ); + mNotebook = new QTabWidget(); + mNotebook->addTab( editorPage, "Editor" ); + mNotebook->addTab( mergePage, "Merge" ); + mNotebook->addTab( printPage, "Print" ); + mNotebook->setEnabled( false ); + + setCentralWidget( mNotebook ); setDocVerbsEnabled( false ); setPasteVerbsEnabled( false ); @@ -114,6 +115,7 @@ namespace glabels mObjectEditor->setModel( mModel ); mPrintView->setModel( mModel ); + mNotebook->setEnabled( true ); setDocVerbsEnabled( true ); setSelectionVerbsEnabled( false ); setMultiSelectionVerbsEnabled( false ); diff --git a/glabels/MainWindow.h b/glabels/MainWindow.h index 05d8bf5..3bce470 100644 --- a/glabels/MainWindow.h +++ b/glabels/MainWindow.h @@ -189,6 +189,7 @@ namespace glabels QToolBar* fileToolBar; QToolBar* editorToolBar; + QTabWidget* mNotebook; LabelModel* mModel; View* mView; ObjectEditor* mObjectEditor; diff --git a/glabels/Preview.cpp b/glabels/Preview.cpp new file mode 100644 index 0000000..439919e --- /dev/null +++ b/glabels/Preview.cpp @@ -0,0 +1,175 @@ +/* Preview.cpp + * + * Copyright (C) 2013 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#include "Preview.h" + +#include "LabelModel.h" + +#include +#include +#include + + +// +// Private Configuration Data +// +namespace +{ + const QColor paperColor( 255, 255, 255 ); + const QColor paperOutlineColor( 0, 0, 0 ); + const double paperOutlineWidthPixels = 1; + + const QColor shadowColor( 64, 64, 64 ); + const double shadowOffsetPixels = 3; + const double shadowRadiusPixels = 12; + + const QColor labelColor( 255, 255, 255 ); + const QColor labelOutlineColor( 215, 215, 215 ); + const double labelOutlineWidthPixels = 1; +} + + +namespace glabels +{ + + /// + /// Constructor + /// + Preview::Preview( QWidget *parent ) + : mModel(0), QGraphicsView(parent) + { + mScene = new QGraphicsScene(); + setScene( mScene ); + + setAttribute(Qt::WA_TranslucentBackground); + viewport()->setAutoFillBackground(false); + + setFrameStyle( QFrame::NoFrame ); + setRenderHints( QPainter::Antialiasing ); + } + + + /// + /// Set model + /// + void Preview::setModel( const LabelModel* model ) + { + mModel = model; + + clearScene(); + + if ( mModel != NULL ) + { + // Set scene up with a 5% margin around paper + double x = -0.05 * mModel->tmplate()->pageWidth(); + double y = -0.05 * mModel->tmplate()->pageHeight(); + double w = 1.10 * mModel->tmplate()->pageWidth(); + double h = 1.10 * mModel->tmplate()->pageHeight(); + + mScene->setSceneRect( x, y, w, h ); + fitInView( mScene->sceneRect(), Qt::KeepAspectRatio ); + + drawPaper( mModel->tmplate()->pageWidth(), mModel->tmplate()->pageHeight() ); + drawLabels(); + } + } + + + /// + /// Resize Event Handler + /// + void Preview::resizeEvent( QResizeEvent* event ) + { + fitInView( mScene->sceneRect(), Qt::KeepAspectRatio ); + } + + + /// + /// Clear View + /// + void Preview::clearScene() + { + foreach ( QGraphicsItem *item, mScene->items() ) + { + mScene->removeItem( item ); + delete item; + } + } + + + /// + /// Draw Paper + /// + void Preview::drawPaper( double pw, double ph ) + { + QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(); + shadowEffect->setColor( shadowColor ); + shadowEffect->setOffset( shadowOffsetPixels ); + shadowEffect->setBlurRadius( shadowRadiusPixels ); + + QBrush brush( paperColor ); + QPen pen( paperOutlineColor ); + pen.setCosmetic( true ); + pen.setWidthF( paperOutlineWidthPixels ); + + QGraphicsRectItem *pageItem = new QGraphicsRectItem( 0, 0, pw, ph ); + pageItem->setBrush( brush ); + pageItem->setPen( pen ); + pageItem->setGraphicsEffect( shadowEffect ); + + mScene->addItem( pageItem ); + } + + + /// + /// Draw Labels on Paper + /// + void Preview::drawLabels() + { + libglabels::Frame *frame = mModel->tmplate()->frames().first(); + + foreach (libglabels::Point origin, frame->getOrigins() ) + { + drawLabel( origin.x(), origin.y(), frame->path() ); + } + } + + + /// + /// Draw a Single Label at x,y + /// + void Preview::drawLabel( double x, double y, const QPainterPath &path ) + { + QBrush brush( labelColor ); + QPen pen( labelOutlineColor ); + pen.setStyle( Qt::DotLine ); + pen.setCosmetic( true ); + pen.setWidthF( labelOutlineWidthPixels ); + + QGraphicsPathItem *labelItem = new QGraphicsPathItem( path ); + labelItem->setBrush( brush ); + labelItem->setPen( pen ); + labelItem->setPos( x, y ); + + mScene->addItem( labelItem ); + } + + +} diff --git a/glabels/Preview.h b/glabels/Preview.h new file mode 100644 index 0000000..7dda97e --- /dev/null +++ b/glabels/Preview.h @@ -0,0 +1,85 @@ +/* Preview.h + * + * Copyright (C) 2013 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#ifndef glabels_Preview_h +#define glabels_Preview_h + +#include +#include + +#include + + +namespace glabels +{ + class LabelModel; // Forward reference + + + /// + /// Preview Widget + /// + class Preview : public QGraphicsView + { + Q_OBJECT + + + ///////////////////////////////// + // Life Cycle + ///////////////////////////////// + public: + Preview( QWidget *parent = 0 ); + + + ///////////////////////////////// + // Properties + ///////////////////////////////// + public: + void setModel( const LabelModel* model ); + + + ///////////////////////////////////// + // Event handlers + ///////////////////////////////////// + protected: + void resizeEvent( QResizeEvent* event ); + + + ///////////////////////////////// + // Internal Methods + ///////////////////////////////// + private: + void clearScene(); + void drawPaper( double pw, double ph ); + void drawLabels(); + void drawLabel( double x, double y, const QPainterPath &path ); + + + ///////////////////////////////// + // Private Data + ///////////////////////////////// + private: + const LabelModel* mModel; + QGraphicsScene* mScene; + + }; + +} + +#endif // glabels_Preview_h diff --git a/glabels/PrintView.cpp b/glabels/PrintView.cpp index 5889d18..cf431eb 100644 --- a/glabels/PrintView.cpp +++ b/glabels/PrintView.cpp @@ -56,7 +56,7 @@ namespace glabels /// void PrintView::onLabelSizeChanged() { - preview->setTemplate( mModel->tmplate() ); + preview->setModel( mModel ); } diff --git a/glabels/SimplePreview.cpp b/glabels/SimplePreview.cpp index 0edda80..8fa1825 100644 --- a/glabels/SimplePreview.cpp +++ b/glabels/SimplePreview.cpp @@ -57,7 +57,7 @@ namespace glabels /// /// Constructor /// - SimplePreview::SimplePreview( QWidget *parent = 0 ) + SimplePreview::SimplePreview( QWidget *parent ) : mTmplate(NULL), mRotateFlag(false), QGraphicsView(parent) { mScene = new QGraphicsScene(); diff --git a/glabels/SimplePreview.h b/glabels/SimplePreview.h index 810780d..64b494d 100644 --- a/glabels/SimplePreview.h +++ b/glabels/SimplePreview.h @@ -44,7 +44,7 @@ namespace glabels // Life Cycle ///////////////////////////////// public: - SimplePreview( QWidget *parent ); + SimplePreview( QWidget *parent = 0 ); ///////////////////////////////// diff --git a/glabels/ui/PrintView.ui b/glabels/ui/PrintView.ui index 8a9785e..317e489 100644 --- a/glabels/ui/PrintView.ui +++ b/glabels/ui/PrintView.ui @@ -26,7 +26,7 @@ QLayout::SetDefaultConstraint - + 0 @@ -43,6 +43,9 @@ + + 16 + @@ -456,9 +459,9 @@ - glabels::SimplePreview + glabels::Preview QWidget -
SimplePreview.h
+
Preview.h
1