From 9dce9addf1a313eb8854604b916c77066ac17670 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Wed, 13 Nov 2013 23:53:05 -0500 Subject: [PATCH] Added SimplePreview to NewLabelDialog. --- app/CMakeLists.txt | 2 + app/NewLabelDialog.cpp | 20 +++++++ app/NewLabelDialog.h | 1 + app/SimplePreview.cpp | 126 +++++++++++++++++++++++++++++++++++++++ app/SimplePreview.h | 56 +++++++++++++++++ app/ui/NewLabelDialog.ui | 7 ++- 6 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 app/SimplePreview.cpp create mode 100644 app/SimplePreview.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index db5b60f..e2a635f 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -18,6 +18,7 @@ set (glabels_sources TemplatePickerItem.cpp TextNode.cpp NewLabelDialog.cpp + SimplePreview.cpp ) set (glabels_qobject_headers @@ -26,6 +27,7 @@ set (glabels_qobject_headers MainWindow.h TemplatePicker.h NewLabelDialog.h + SimplePreview.h ) set (glabels_forms diff --git a/app/NewLabelDialog.cpp b/app/NewLabelDialog.cpp index f6e2fed..1c41624 100644 --- a/app/NewLabelDialog.cpp +++ b/app/NewLabelDialog.cpp @@ -21,6 +21,7 @@ #include "NewLabelDialog.h" #include "libglabels/Db.h" +#include "TemplatePickerItem.h" namespace gLabels @@ -48,6 +49,7 @@ namespace gLabels connect( pageSizeUsRadio, SIGNAL(toggled(bool)), this, SLOT(pageSizeRadioToggled(bool)) ); connect( pageSizeOtherRadio, SIGNAL(toggled(bool)), this, SLOT(pageSizeRadioToggled(bool)) ); + connect( templatePicker, SIGNAL(itemSelectionChanged()), this, SLOT(templatePickerSelectionChanged()) ); } @@ -71,4 +73,22 @@ namespace gLabels } } + + void NewLabelDialog::templatePickerSelectionChanged() + { + QList selectedItems = templatePicker->selectedItems(); + + if ( selectedItems.isEmpty() ) + { + // Clear preview + simplePreview->setTemplate( NULL ); + } + else + { + // Set template to preview + TemplatePickerItem *tItem = dynamic_cast(selectedItems.first()); + simplePreview->setTemplate( tItem->tmplate() ); + } + } + } diff --git a/app/NewLabelDialog.h b/app/NewLabelDialog.h index d2d1d69..1d71fe7 100644 --- a/app/NewLabelDialog.h +++ b/app/NewLabelDialog.h @@ -37,6 +37,7 @@ namespace gLabels private slots: void searchEntryTextChanged( const QString &text ); void pageSizeRadioToggled( bool checked ); + void templatePickerSelectionChanged(); }; diff --git a/app/SimplePreview.cpp b/app/SimplePreview.cpp new file mode 100644 index 0000000..17d19f2 --- /dev/null +++ b/app/SimplePreview.cpp @@ -0,0 +1,126 @@ +/* SimplePreview.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 "SimplePreview.h" + +#include +#include + +namespace +{ + const QColor paperColor( 255, 255, 255 ); + const QColor paperOutlineColor( 0, 0, 0 ); + + const QColor shadowColor( 64, 64, 64 ); + + const QColor labelColor( 242, 242, 242 ); + const QColor labelOutlineColor( 64, 64, 64 ); +} + + +namespace gLabels +{ + + SimplePreview::SimplePreview( QWidget *parent = 0 ) : QGraphicsView(parent) + { + mScene = new QGraphicsScene(); + setScene( mScene ); + + setAttribute(Qt::WA_TranslucentBackground); + viewport()->setAutoFillBackground(false); + + setFrameStyle( QFrame::NoFrame ); + } + + + void SimplePreview::setTemplate( const libglabels::Template *tmplate ) + { + clearScene(); + + if ( tmplate != NULL ) + { + double x = -0.05 * tmplate->pageWidth(); + double y = -0.05 * tmplate->pageHeight(); + double w = 1.10 * tmplate->pageWidth(); + double h = 1.10 * tmplate->pageHeight(); + + mScene->setSceneRect( x, y, w, h ); + fitInView( x, y, w, h, Qt::KeepAspectRatio ); + + drawPaper( tmplate->pageWidth(), tmplate->pageHeight() ); + drawLabels( tmplate ); + } + } + + + void SimplePreview::clearScene() + { + foreach ( QGraphicsItem *item, mScene->items() ) + { + mScene->removeItem( item ); + delete item; + } + } + + + void SimplePreview::drawPaper( double pw, double ph ) + { + QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(); + shadowEffect->setColor( shadowColor ); + shadowEffect->setOffset( 0.005*pw ); + shadowEffect->setBlurRadius( 0.035*pw ); + + QBrush brush( paperColor ); + QPen pen( paperOutlineColor ); + + QGraphicsRectItem *pageItem = new QGraphicsRectItem( 0, 0, pw, ph ); + pageItem->setBrush( brush ); + pageItem->setPen( pen ); + pageItem->setGraphicsEffect( shadowEffect ); + + mScene->addItem( pageItem ); + } + + + void SimplePreview::drawLabels( const libglabels::Template *tmplate ) + { + libglabels::Frame *frame = tmplate->frames().first(); + + foreach (libglabels::Point origin, frame->getOrigins() ) + { + drawLabel( origin.x(), origin.y(), frame->path() ); + } + } + + + void SimplePreview::drawLabel( double x, double y, const QPainterPath &path ) + { + QBrush brush( labelColor ); + QPen pen( labelOutlineColor ); + + QGraphicsPathItem *labelItem = new QGraphicsPathItem( path ); + labelItem->setBrush( brush ); + labelItem->setPen( pen ); + labelItem->setPos( x, y ); + + mScene->addItem( labelItem ); + } + +} diff --git a/app/SimplePreview.h b/app/SimplePreview.h new file mode 100644 index 0000000..55402a1 --- /dev/null +++ b/app/SimplePreview.h @@ -0,0 +1,56 @@ +/* SimplePreview.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_SimplePreview_h +#define glabels_SimplePreview_h + +#include +#include + +#include + +#include "libglabels/Template.h" + + +namespace gLabels +{ + + class SimplePreview : public QGraphicsView + { + Q_OBJECT + + public: + SimplePreview( QWidget *parent ); + + void setTemplate( const libglabels::Template *tmplate ); + + private: + void clearScene(); + void drawPaper( double pw, double ph ); + void drawLabels( const libglabels::Template *tmplate ); + void drawLabel( double x, double y, const QPainterPath &path ); + + QGraphicsScene *mScene; + + }; + +} + +#endif // glabels_SimplePreview_h diff --git a/app/ui/NewLabelDialog.ui b/app/ui/NewLabelDialog.ui index f8bf6a7..ab87740 100644 --- a/app/ui/NewLabelDialog.ui +++ b/app/ui/NewLabelDialog.ui @@ -140,7 +140,7 @@ - + 0 @@ -273,6 +273,11 @@ QListWidget
TemplatePicker.h
+ + gLabels::SimplePreview + QGraphicsView +
SimplePreview.h
+