diff --git a/app/MainWindow.cpp b/app/MainWindow.cpp index e191720..510cbd2 100644 --- a/app/MainWindow.cpp +++ b/app/MainWindow.cpp @@ -222,12 +222,12 @@ namespace glabels viewGridAction = new QAction( tr("Grid"), this ); viewGridAction->setCheckable( true ); viewGridAction->setStatusTip( tr("Change visibility of the grid in current window") ); - connect( viewGridAction, SIGNAL(triggered()), this, SLOT(viewGrid()) ); + connect( viewGridAction, SIGNAL(toggled(bool)), this, SLOT(viewGrid(bool)) ); viewMarkupAction = new QAction( tr("Markup"), this ); viewMarkupAction->setCheckable( true ); viewMarkupAction->setStatusTip( tr("Change visibility of markup lines in current window") ); - connect( viewMarkupAction, SIGNAL(triggered()), this, SLOT(viewMarkup()) ); + connect( viewMarkupAction, SIGNAL(toggled(bool)), this, SLOT(viewMarkup(bool)) ); viewZoomInAction = new QAction( tr("Zoom &In"), this ); viewZoomInAction->setIcon( QIcon::fromTheme( "zoom-in", Icons::Fallback::ZoomIn() ) ); @@ -637,17 +637,20 @@ namespace glabels bool showObjectsToolBar = settings.value( "showObjectsToolBar", true ).toBool(); bool showEditToolBar = settings.value( "showEditToolBar", true ).toBool(); bool showViewToolBar = settings.value( "showViewToolBar", true ).toBool(); + bool showGrid = settings.value( "showGrid", true ).toBool(); settings.endGroup(); viewFileToolBarAction->setChecked( showFileToolBar ); viewObjectsToolBarAction->setChecked( showObjectsToolBar ); viewEditToolBarAction->setChecked( showEditToolBar ); viewViewToolBarAction->setChecked( showViewToolBar ); + viewGridAction->setChecked( showGrid ); fileToolBar->setVisible( showFileToolBar ); objectsToolBar->setVisible( showObjectsToolBar ); editToolBar->setVisible( showEditToolBar ); viewToolBar->setVisible( showViewToolBar ); + view->setGridVisible( showGrid ); } @@ -660,6 +663,7 @@ namespace glabels settings.setValue( "showObjectsToolBar", viewObjectsToolBarAction->isChecked() ); settings.setValue( "showEditToolBar", viewEditToolBarAction->isChecked() ); settings.setValue( "showViewToolBar", viewViewToolBarAction->isChecked() ); + settings.setValue( "showGrid", viewGridAction->isChecked() ); settings.endGroup(); } @@ -796,13 +800,13 @@ namespace glabels } - void MainWindow::viewGrid() + void MainWindow::viewGrid( bool state ) { - std::cout << "ACTION: edit->Grid" << std::endl; + view->setGridVisible( state ); } - void MainWindow::viewMarkup() + void MainWindow::viewMarkup( bool state ) { std::cout << "ACTION: edit->Markup" << std::endl; } diff --git a/app/MainWindow.h b/app/MainWindow.h index 8955ec2..c046f84 100644 --- a/app/MainWindow.h +++ b/app/MainWindow.h @@ -72,8 +72,8 @@ namespace glabels void viewObjectsToolBar( bool ); void viewEditToolBar( bool ); void viewViewToolBar( bool ); - void viewGrid(); - void viewMarkup(); + void viewGrid( bool ); + void viewMarkup( bool ); void viewZoomIn(); void viewZoomOut(); void viewZoom1To1(); diff --git a/app/View.cpp b/app/View.cpp index ecc9b38..a1855d8 100644 --- a/app/View.cpp +++ b/app/View.cpp @@ -21,10 +21,13 @@ #include "View.h" #include +#include #include #include #include +#include "libglabels/FrameRect.h" + namespace { @@ -40,6 +43,10 @@ namespace const QColor labelColor( 255, 255, 255 ); const QColor labelOutlineColor( 0, 0, 0 ); const double labelOutlineWidthPixels = 1; + + const QColor gridLineColor( 192, 192, 192 ); + const double gridLineWidthPixels = 1; + const double gridSpacing = 9; // TODO: determine from locale. } @@ -58,6 +65,11 @@ namespace glabels mScene = new QGraphicsScene(); setScene( mScene ); + + mScene->addItem( mLabelLayer = new QGraphicsItemGroup() ); + mScene->addItem( mGridLayer = new QGraphicsItemGroup() ); + mScene->addItem( mObjectLayer = new QGraphicsItemGroup() ); + mScene->addItem( mForegroundLayer = new QGraphicsItemGroup() ); } @@ -66,12 +78,20 @@ namespace glabels mModel = model; createLabelLayer(); + createGridLayer(); foreach (LabelModelObject* object, model->objectList() ) { - QGraphicsItem* item = object->createGraphicsItem(); - mScene->addItem( item ); + addObjectToObjectLayer( object ); } + + createForegroundLayer(); + } + + + void View::setGridVisible( bool visibleFlag ) + { + mGridLayer->setVisible( visibleFlag ); } @@ -191,26 +211,97 @@ namespace glabels } + void View::clearLayer( QGraphicsItemGroup* layer ) + { + foreach( QGraphicsItem* item, layer->childItems() ) + { + layer->removeFromGroup( item ); + } + } + + void View::createLabelLayer() { + clearLayer( mLabelLayer ); + QGraphicsPathItem *labelItem = new QGraphicsPathItem( mModel->frame()->path() ); QBrush brush( labelColor ); labelItem->setBrush( brush ); - QPen pen( labelOutlineColor ); - pen.setJoinStyle( Qt::MiterJoin ); - pen.setCosmetic( true ); - pen.setWidthF( labelOutlineWidthPixels ); - labelItem->setPen( pen ); - QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(); shadowEffect->setColor( shadowColor ); shadowEffect->setOffset( shadowOffsetPixels ); shadowEffect->setBlurRadius( shadowRadiusPixels ); labelItem->setGraphicsEffect( shadowEffect ); - mScene->addItem( labelItem ); + mLabelLayer->addToGroup( labelItem ); + } + + + void View::createGridLayer() + { + clearLayer( mGridLayer ); + + QGraphicsPathItem *clipItem = new QGraphicsPathItem( mModel->frame()->path() ); + clipItem->setFlag( QGraphicsItem::ItemClipsChildrenToShape ); + + QPen pen( gridLineColor ); + pen.setCosmetic( true ); + pen.setWidthF( gridLineWidthPixels ); + + double w = mModel->w(); + double h = mModel->h(); + double x0; + double y0; + if ( dynamic_cast( mModel->frame() ) ) + { + x0 = gridSpacing; + y0 = gridSpacing; + } + else + { + /* round labels, align grid with center of label. */ + x0 = fmod( w/2, gridSpacing ); + y0 = fmod( h/2, gridSpacing ); + } + + for ( double x = x0; x < w; x += gridSpacing ) + { + QGraphicsLineItem* lineItem = new QGraphicsLineItem( x, 0, x, h, clipItem ); + lineItem->setPen( pen ); + } + + for ( double y = y0; y < h; y += gridSpacing ) + { + QGraphicsLineItem* lineItem = new QGraphicsLineItem( 0, y, w, y, clipItem ); + lineItem->setPen( pen ); + } + + mGridLayer->addToGroup( clipItem ); + } + + + void View::addObjectToObjectLayer( LabelModelObject* object ) + { + QGraphicsItem* item = object->createGraphicsItem(); + mObjectLayer->addToGroup( item ); + } + + + void View::createForegroundLayer() + { + clearLayer( mForegroundLayer ); + + QGraphicsPathItem *outlineItem = new QGraphicsPathItem( mModel->frame()->path() ); + + QPen pen( labelOutlineColor ); + pen.setJoinStyle( Qt::MiterJoin ); + pen.setCosmetic( true ); + pen.setWidthF( labelOutlineWidthPixels ); + outlineItem->setPen( pen ); + + mForegroundLayer->addToGroup( outlineItem ); } } diff --git a/app/View.h b/app/View.h index 0aea061..9b458d2 100644 --- a/app/View.h +++ b/app/View.h @@ -23,6 +23,7 @@ #include #include +#include #include "LabelModel.h" @@ -64,6 +65,13 @@ namespace glabels void setModel( LabelModel* model ); + ///////////////////////////////////// + // Visibility operations + ///////////////////////////////////// + public: + void setGridVisible( bool visibleFlag ); + + ///////////////////////////////////// // Zoom operations ///////////////////////////////////// @@ -91,19 +99,28 @@ namespace glabels // Private methods ///////////////////////////////////// private: + void clearLayer( QGraphicsItemGroup* layer ); void createLabelLayer(); + void createGridLayer(); + void addObjectToObjectLayer( LabelModelObject* object ); + void createForegroundLayer(); ///////////////////////////////////// // Private data ///////////////////////////////////// private: - QGraphicsScene* mScene; + QGraphicsScene* mScene; - double mZoom; - bool mZoomToFitFlag; + QGraphicsItemGroup* mLabelLayer; + QGraphicsItemGroup* mGridLayer; + QGraphicsItemGroup* mObjectLayer; + QGraphicsItemGroup* mForegroundLayer; - LabelModel* mModel; + double mZoom; + bool mZoomToFitFlag; + + LabelModel* mModel; };