* scene.cpp: Creation of the function "selectAll" to select all items in the
scene,
* mainwindow.cpp: User can now have a previsualization of the printing and print in a
dialog,
* xmlview.cpp.
---
GScribo/GScribo.pro.user | 2 +-
GScribo/PagesWidget/pagesmodel.cpp | 2 +
GScribo/PagesWidget/pageswidget.cpp | 14 ++-
GScribo/Rendering/graphicsview.cpp | 6 +-
GScribo/Rendering/scene.cpp | 44 +++++---
GScribo/Rendering/scene.h | 8 +-
GScribo/XmlWidget/xmlview.cpp | 3 +
GScribo/XmlWidget/xmlwidget.cpp | 2 +-
GScribo/mainwindow.cpp | 209 +++++++++++++++++++++++++++--------
GScribo/mainwindow.h | 23 +++-
GScribo/xml.cpp | 4 +-
11 files changed, 237 insertions(+), 80 deletions(-)
diff --git a/GScribo/GScribo.pro.user b/GScribo/GScribo.pro.user
index 05080d9..6c526e8 100644
--- a/GScribo/GScribo.pro.user
+++ b/GScribo/GScribo.pro.user
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
-<!-- Written by Qt Creator 2.4.1, 2013-02-15T03:38:38. -->
+<!-- Written by Qt Creator 2.4.1, 2013-02-15T08:01:13. -->
<qtcreator>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
diff --git a/GScribo/PagesWidget/pagesmodel.cpp b/GScribo/PagesWidget/pagesmodel.cpp
index 82bf581..faccb74 100644
--- a/GScribo/PagesWidget/pagesmodel.cpp
+++ b/GScribo/PagesWidget/pagesmodel.cpp
@@ -48,6 +48,8 @@ void PagesModel::removePixmap(const QModelIndex &parent)
void PagesModel::setCurrentRow(int currentRow)
{
beginResetModel();
+
currentRow_ = currentRow;
+
endResetModel();
}
diff --git a/GScribo/PagesWidget/pageswidget.cpp b/GScribo/PagesWidget/pageswidget.cpp
index f12c713..400046a 100644
--- a/GScribo/PagesWidget/pageswidget.cpp
+++ b/GScribo/PagesWidget/pageswidget.cpp
@@ -21,20 +21,22 @@ PagesWidget::PagesWidget(QWidget *parent):
void PagesWidget::removeSelection()
{
- if(hasFocus() && selectedIndexes().count() > 0)
+ QModelIndexList indexes = selectionModel()->selectedIndexes();
+ if(hasFocus() && indexes.count() > 0)
{
// Sort the list because after the first removal, the second row index may not be
the good one.
- qSort(selectedIndexes().begin(), selectedIndexes().end());
+ qSort(indexes.begin(), indexes.end());
// From the highest row to the smallest to be sure to remove the good index.
- for(int i = selectedIndexes().count()-1; i > -1; i--)
- model_.removePixmap(selectedIndexes()[i]);
+ for(int i = indexes.count()-1; i > -1; i--)
+ model_.removePixmap(indexes[i]);
// Draw new image on the scene.
- getPicture(model_.index(selectedIndexes()[0].row()));
+ getPicture(model_.index(indexes[0].row()));
clearSelection();
- model_.setCurrentRow(selectedIndexes()[0].row());
+ model_.setCurrentRow(indexes[0].row());
+ scrollTo(indexes[0]);
}
}
diff --git a/GScribo/Rendering/graphicsview.cpp b/GScribo/Rendering/graphicsview.cpp
index b1da6f5..7a05cd5 100644
--- a/GScribo/Rendering/graphicsview.cpp
+++ b/GScribo/Rendering/graphicsview.cpp
@@ -20,7 +20,7 @@ void GraphicsView::init()
setTransformationAnchor(GraphicsView::AnchorUnderMouse);
setBackgroundBrush(QBrush(Qt::lightGray));
- // Important to hide scroll bars : when they appear, they trigger the resizeEvent.
+ // Important to hide scroll bars : when they appear, it triggers the resizeEvent.
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
@@ -36,6 +36,7 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseButtonPress, event->pos(),
Qt::LeftButton,
Qt::LeftButton, Qt::MetaModifier);
+ event->ignore();
QGraphicsView::mousePressEvent(mouseEvent);
}
@@ -123,6 +124,7 @@ void GraphicsView::keyPressEvent(QKeyEvent* event)
void GraphicsView::fitInView(const QRectF& rect)
{
+ setFocus();
isWidthGreater_ = static_cast<qreal>(rect.width()) /
static_cast<qreal>(rect.height()) > 1 ? true :
false;
qreal ratio = isWidthGreater_ ? static_cast<qreal>(width()) /
static_cast<qreal>(rect.width()) :
@@ -132,7 +134,7 @@ void GraphicsView::fitInView(const QRectF& rect)
if(isContainedInView_)
{
scaleRatio_ = QSizeF(1, 1);
- scale(ratio, ratio);
+ QGraphicsView::fitInView(rect, Qt::KeepAspectRatio);
}
}
diff --git a/GScribo/Rendering/scene.cpp b/GScribo/Rendering/scene.cpp
index 47b1cd5..6ab8e3c 100644
--- a/GScribo/Rendering/scene.cpp
+++ b/GScribo/Rendering/scene.cpp
@@ -22,6 +22,7 @@ void Scene::init()
{
isPressing_ = false;
isSelectionDisabled_ = false;
+ backgroundPixmap_ = 0;
root_ = 0;
selection_.hide();
@@ -31,13 +32,14 @@ void Scene::init()
void Scene::clearAll()
{
if(root_)
- {
- delete backgroundPixmap;
delete root_;
- }
+
+ if(backgroundPixmap_)
+ delete backgroundPixmap_;
selectedRegions_.clear();
- backgroundPixmap = 0;
+ backgroundPath_ = QString();
+ backgroundPixmap_ = 0;
root_ = 0;
}
@@ -66,9 +68,20 @@ void Scene::clearSelection()
emit selectionCleared();
}
+void Scene::selectAll()
+{
+ RegionItem *item;
+ foreach(QGraphicsItem *child, root_->childItems())
+ {
+ item = static_cast<RegionItem *>(child);
+ item->select();
+ selectedRegions_ << item;
+ }
+}
+
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
- if(root_ && event->button() == Qt::LeftButton &&
!isSelectionDisabled_)
+ if(!backgroundPath_.isEmpty() && event->button() == Qt::LeftButton
&& !isSelectionDisabled_)
{
isPressing_ = true;
@@ -240,19 +253,22 @@ void Scene::setRoot(RootGraphicsItem *root)
void Scene::changeScene(const QString& filename, RootGraphicsItem *root)
{
- backgroundPath_ = filename;
-
// Delete all items.
clearAll();
+ backgroundPath_ = filename;
- QPixmap pixmap(filename);
- setSceneRect(pixmap.rect());
-
- // Create the background item and dock it at the maximal depth of the scene.
- backgroundPixmap = new QGraphicsPixmapItem(pixmap, 0, this);
- backgroundPixmap->setZValue(INT_MIN);
+ if(!filename.isEmpty())
+ {
+ QPixmap pixmap(filename);
+ setSceneRect(pixmap.rect());
+ // Create the background item and dock it at the maximal depth of the scene.
+ backgroundPixmap_ = new QGraphicsPixmapItem(pixmap, 0, this);
+ backgroundPixmap_->setZValue(INT_MIN);
+ }
+ else
+ setSceneRect(QRectF());
- // Add new items.
+ // Add new items.
if(root)
setRoot(root);
}
diff --git a/GScribo/Rendering/scene.h b/GScribo/Rendering/scene.h
index ec71290..129359b 100644
--- a/GScribo/Rendering/scene.h
+++ b/GScribo/Rendering/scene.h
@@ -22,6 +22,8 @@ class Scene :
explicit Scene(qreal x, qreal y, qreal width, qreal height, QObject *parent =
0);
inline QString backgroundPath() const;
+ inline QGraphicsPixmapItem *backgroundPixmap() const;
+
inline QList<RegionItem *> selectedRegions() const;
void setRoot(RootGraphicsItem *rootItem);
@@ -29,6 +31,7 @@ class Scene :
void select(const QRectF& rect);
void select(const QPointF& point);
+ void selectAll();
inline bool selectionEnable() const;
inline void setSelectionEnable(bool enable);
@@ -48,7 +51,7 @@ class Scene :
RootGraphicsItem *root_;
QString backgroundPath_;
- QGraphicsPixmapItem *backgroundPixmap;
+ QGraphicsPixmapItem *backgroundPixmap_;
Selection selection_;
QList<RegionItem *> selectedRegions_;
@@ -82,6 +85,9 @@ class Scene :
inline QString Scene::backgroundPath() const
{ return backgroundPath_; }
+inline QGraphicsPixmapItem *Scene::backgroundPixmap() const
+{ return backgroundPixmap_; }
+
inline RootGraphicsItem *Scene::root() const
{ return root_; }
diff --git a/GScribo/XmlWidget/xmlview.cpp b/GScribo/XmlWidget/xmlview.cpp
index 4bd072a..70a79b7 100644
--- a/GScribo/XmlWidget/xmlview.cpp
+++ b/GScribo/XmlWidget/xmlview.cpp
@@ -79,7 +79,10 @@ void XmlView::selectionChanged(const QItemSelection& selected,
const QItemSelect
emit loadAttributes(xmlItems.last()->attributes());
}
else
+ {
+ emit resetProperty();
emit emptySelection();
+ }
emit newSelection();
}
diff --git a/GScribo/XmlWidget/xmlwidget.cpp b/GScribo/XmlWidget/xmlwidget.cpp
index 8584052..9cddaef 100644
--- a/GScribo/XmlWidget/xmlwidget.cpp
+++ b/GScribo/XmlWidget/xmlwidget.cpp
@@ -18,7 +18,7 @@ XmlWidget::XmlWidget(QWidget *parent) :
addWidget(&view_);
addWidget(&attributes_);
- connect(&view_, SIGNAL(resetProperty()), &attributes_, SLOT(reset()));
+ connect(&view_, SIGNAL(resetProperty()), &attributesModel_, SLOT());
connect(&view_, SIGNAL(clicked(QModelIndex)), this,
SLOT(loadAttributes(QModelIndex)));
connect(&view_, SIGNAL(loadAttributes(XmlAttributes)), &attributesModel_,
SLOT(load(XmlAttributes)));
}
diff --git a/GScribo/mainwindow.cpp b/GScribo/mainwindow.cpp
index f7d7630..9e17421 100644
--- a/GScribo/mainwindow.cpp
+++ b/GScribo/mainwindow.cpp
@@ -1,4 +1,4 @@
- #include "mainwindow.h"
+#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
@@ -9,9 +9,9 @@ MainWindow::MainWindow(QWidget *parent) :
setWindowTitle(tr("GScribo"));
statusBar()->hide();
+ ui->mainToolBar->hide();
initGraphicsRegion();
- initTextRegion();
initXmlWidget();
initRegionWidget();
initPageWidget();
@@ -80,16 +80,6 @@ void MainWindow::initXmlWidget()
dockXml_.setVisible(false);
}
-void MainWindow::initTextRegion()
-{
- dockText_.setWindowTitle("Text");
- dockText_.setFeatures(QDockWidget::DockWidgetFloatable |
QDockWidget::DockWidgetMovable);
- dockText_.setWidget(&textEdit_);
-
- addDockWidget(Qt::RightDockWidgetArea, &dockText_);
- dockText_.hide();
-}
-
void MainWindow::initMenuBar()
{
QMenu *menuFile = ui->menuBar->addMenu(tr("File"));
@@ -97,8 +87,30 @@ void MainWindow::initMenuBar()
QAction *open = menuFile->addAction(tr("Open"));
connect(open, SIGNAL(triggered()), SLOT(onOpen()));
- QAction *segment = menuFile->addAction(tr("Segment"));
- connect(segment, SIGNAL(triggered()), this, SLOT(onSegment()));
+ segment_ = menuFile->addAction(tr("Segment"));
+ segment_->setEnabled(false);
+ connect(segment_, SIGNAL(triggered()), this, SLOT(onSegment()));
+
+ menuFile->addSeparator();
+
+ previewPrinting_ = menuFile->addAction(tr("Preview Printing"));
+ previewPrinting_->setEnabled(false);
+ connect(previewPrinting_, SIGNAL(triggered()), this, SLOT(onPreviewPrint()));
+
+ print_ = menuFile->addAction(tr("Print"));
+ print_->setEnabled(false);
+ connect(print_, SIGNAL(triggered()), this, SLOT(onPrint()));
+
+ menuFile->addSeparator();
+
+ export_ = menuFile->addAction(tr("Exportation"));
+ export_->setEnabled(false);
+ connect(export_, SIGNAL(triggered()), this, SLOT(onExportation()));
+
+ menuFile->addSeparator();
+
+ QAction *quit = menuFile->addAction(tr("Quit"));
+ connect(quit, SIGNAL(triggered()), this, SLOT(close()));
QMenu *menuAreas = ui->menuBar->addMenu(tr("Areas"));
@@ -124,6 +136,22 @@ void MainWindow::initMenuBar()
connect(about, SIGNAL(triggered()), SLOT(onAbout()));
}
+void MainWindow::setActionsEnabled(bool isSegmented)
+{
+ segment_->setEnabled(!isSegmented);
+ print_->setEnabled(isSegmented);
+ previewPrinting_->setEnabled(isSegmented);
+ export_->setEnabled(isSegmented);
+}
+
+void MainWindow::disableActions()
+{
+ segment_->setEnabled(false);
+ print_->setEnabled(false);
+ previewPrinting_->setEnabled(false);
+ export_->setEnabled(false);
+}
+
void MainWindow::connectWidgets()
{
// Each time the scene rect change (when a new picture is loaded), we fit the scene
background rectangle in the view.
@@ -200,18 +228,90 @@ void MainWindow::onOpen()
void MainWindow::onSegment()
{
- if(scene_.backgroundPath() != "")
- {
- QStringList filenames;
+ QStringList filenames;
- if(!pagesWidget_.isVisible())
- filenames << scene_.backgroundPath();
- else
- filenames = pagesWidget_.filenames();
+ if(!pagesWidget_.isVisible())
+ filenames << scene_.backgroundPath();
+ else
+ filenames = pagesWidget_.filenames();
+
+ // Run segmentation of page(s).
+ //progressDialog.reset();
+ //runner.start_demat(filenames);
+}
+
+void MainWindow::onPreviewPrint()
+{
+ QPrinter printer(QPrinter::HighResolution);
+ printer.setPaperSize(QPrinter::A4);
+ printer.setResolution(300);
+
+ QPrintPreviewDialog preview(&printer);
+ connect(&preview, SIGNAL(paintRequested(QPrinter*)), this,
SLOT(printScene(QPrinter*)));
+
+ preview.exec();
+}
+
+void MainWindow::onPrint()
+{
+ QPrinter printer(QPrinter::HighResolution);
+ printer.setPaperSize(QPrinter::A4);
+ printer.setResolution(300);
+
+ QPrintDialog dialog(&printer);
+ dialog.setWindowTitle("Print Document");
+
+ if(dialog.exec() != QDialog::Accepted)
+ return;
+
+ printScene(&printer);
+}
+
+void MainWindow::printScene(QPrinter *printer)
+{
+ scene_.selectAll();
+
+ QPainter painter(printer);
+ QStyleOptionGraphicsItem options;
+
+ scene_.backgroundPixmap()->paint(&painter, &options, 0);
+
+ // Paint backwards items first.
+ printItems(&painter, scene_.root()->childsFrom(GraphicsRegion::TextRegion),
&options);
+ printItems(&painter, scene_.root()->childsFrom(GraphicsRegion::Image),
&options);
+
+ for(int i = GraphicsRegion::Line; i < GraphicsRegion::Image; i++)
+ printItems(&painter,
scene_.root()->childsFrom(static_cast<GraphicsRegion::Id>(i)), &options);
- // Run segmentation of page(s).
- //progressDialog.reset();
- //runner.start_demat(filenames);
+ for(int i = GraphicsRegion::Noise; i <= GraphicsRegion::Meanline; i++)
+ printItems(&painter,
scene_.root()->childsFrom(static_cast<GraphicsRegion::Id>(i)), &options);
+
+ scene_.clearSelection();
+}
+
+void MainWindow::onExportation()
+{
+ QFileInfo fileInfo(scene_.backgroundPath());
+ QString outputSuggestion = fileInfo.baseName() + ".pdf";
+ QString output = QFileDialog::getSaveFileName(0, tr("Export Document As
..."), outputSuggestion,
+ tr("PDF (*.pdf);; HTML (*.html
*.htm"));
+
+ if(!output.isEmpty())
+ {
+ progressDialog_.reset();
+ //runner_.start_export(scene_.backgroundPath(), xml_.filename(), output);
+ }
+}
+
+void MainWindow::printItems(QPainter *painter, const QList<QGraphicsItem *>&
items, QStyleOptionGraphicsItem *options)
+{
+ foreach(QGraphicsItem *child, items)
+ {
+ QRect viewport = scene_.backgroundPixmap()->mapRectFromItem(child,
child->boundingRect()).toRect();
+ painter->translate(abs(child->boundingRect().x() - viewport.x()),
+ abs(child->boundingRect().y() - viewport.y()));
+ child->paint(painter, options);
+ painter->resetTransform();
}
}
@@ -232,6 +332,8 @@ void MainWindow::onXmlSaved(const QString& filename)
xml_.load(filename);
xmlWidget_.changeView(xml_.xmlItem());
scene_.setRoot(xml_.graphicsItem());
+
+ setActionsEnabled(true);
}
void MainWindow::onFileChanged(const QString& filename)
@@ -240,9 +342,20 @@ void MainWindow::onFileChanged(const QString& filename)
if(scene_.backgroundPath() != filename)
{
QString xmlPath = Xml::getPath(filename);
+
+ if(filename.isEmpty())
+ {
+ xmlPath = QString();
+ disableActions();
+ }
// Check if the xml file already exists.
- if(!QFile(xmlPath).exists())
+ else if(!QFile(xmlPath).exists())
+ {
xmlPath = QString();
+ setActionsEnabled(false);
+ }
+ else
+ setActionsEnabled(true);
xml_.load(xmlPath);
scene_.changeScene(filename, xml_.graphicsItem());
@@ -250,29 +363,6 @@ void MainWindow::onFileChanged(const QString& filename)
}
}
-QList<RegionItem *> MainWindow::toRegionItems(QList<XmlItem *> xmlItems)
const
-{
- QList<RegionItem *> regionItems;
- XmlItem *child;
- foreach(child, xmlItems)
- {
- if(child->regionItem())
- regionItems << child->regionItem();
- }
-
- return regionItems;
-}
-
-QList<XmlItem *> MainWindow::toXmlItems(QList<RegionItem *> regionItems)
const
-{
- QList<XmlItem *> xmlItems;
- RegionItem *child;
- foreach(child, regionItems)
- xmlItems << child->xmlItem();
-
- return xmlItems;
-}
-
void MainWindow::onRegionSelection(QList<RegionItem *> regionItems)
{
if(!regionItems.isEmpty())
@@ -297,3 +387,26 @@ void MainWindow::onXmlChangeSelection(QList<XmlItem *>
xmlItems, bool select)
scene_.unselect(regionItems);
}
}
+
+QList<RegionItem *> MainWindow::toRegionItems(QList<XmlItem *> xmlItems)
const
+{
+ QList<RegionItem *> regionItems;
+ XmlItem *child;
+ foreach(child, xmlItems)
+ {
+ if(child->regionItem())
+ regionItems << child->regionItem();
+ }
+
+ return regionItems;
+}
+
+QList<XmlItem *> MainWindow::toXmlItems(QList<RegionItem *> regionItems)
const
+{
+ QList<XmlItem *> xmlItems;
+ RegionItem *child;
+ foreach(child, regionItems)
+ xmlItems << child->xmlItem();
+
+ return xmlItems;
+}
diff --git a/GScribo/mainwindow.h b/GScribo/mainwindow.h
index 4d33fee..01fb252 100644
--- a/GScribo/mainwindow.h
+++ b/GScribo/mainwindow.h
@@ -1,13 +1,15 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
+#include <QPrintPreviewDialog>
#include <QGraphicsView>
-#include <QPlainTextEdit>
+#include <QPrintDialog>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QDockWidget>
#include <QFileDialog>
#include <QGroupBox>
+#include <QPrinter>
#include "Preferences/preferencesdialog.h"
#include "Processing/progressdialog.h"
@@ -40,13 +42,18 @@ class MainWindow:
private:
void initGraphicsRegion();
- void initTextRegion();
void initPageWidget();
void initRegionWidget();
void initXmlWidget();
void initMenuBar();
void connectWidgets();
+ void setActionsEnabled(bool isSegmented);
+ void disableActions();
+
+ void printScene(QPrinter *printer);
+ void printItems(QPainter *painter, const QList<QGraphicsItem *>& items,
QStyleOptionGraphicsItem *options);
+
Xml processTmpXml(const QString& filename) const;
QList<RegionItem *> toRegionItems(QList<XmlItem *> regionItems)
const;
QList<XmlItem *> toXmlItems(QList<RegionItem *> xmlItems) const;
@@ -55,7 +62,6 @@ class MainWindow:
DockWidget dockRegion_;
DockWidget dockPages_;
- DockWidget dockText_;
DockWidget dockXml_;
GraphicsView graphicsView_;
@@ -64,17 +70,23 @@ class MainWindow:
PagesWidget pagesWidget_;
RegionWidget regionWidget_;
- QPlainTextEdit textEdit_;
-
ProgressDialog progressDialog_;
//Runner runner_;
XmlWidget xmlWidget_;
Xml xml_;
+ QAction *segment_;
+ QAction *previewPrinting_;
+ QAction *print_;
+ QAction *export_;
+
private slots:
void onOpen();
void onSegment();
+ void onPreviewPrint();
+ void onPrint();
+ void onExportation();
void onPreferences();
void onAbout();
@@ -82,6 +94,7 @@ class MainWindow:
void onFileChanged(const QString& filename);
void onRegionSelection(QList<RegionItem *> regionItems);
+
void onXmlChangeSelection(QList<XmlItem *> xmlItems, bool select);
inline void onXmlSelect(QList<XmlItem *> xmlItems);
inline void onXmlUnselect(QList<XmlItem *> xmlItems);
diff --git a/GScribo/xml.cpp b/GScribo/xml.cpp
index 00d6c56..05dfe7f 100644
--- a/GScribo/xml.cpp
+++ b/GScribo/xml.cpp
@@ -2,8 +2,6 @@
Xml::Xml(const QString& filename)
{
- filename_ = filename;
-
load(filename);
}
@@ -28,12 +26,14 @@ void Xml::load(const QString& filename)
{
xmlItem_ = 0;
graphicsItem_ = 0;
+ filename_ = QString();
return;
}
// Reset graphic and tree items;
xmlItem_ = new XmlItem;
graphicsItem_ = new RootGraphicsItem(13);
+ filename_ = getPath(filename);
QFile xmlFile(filename);
xmlFile.open(QIODevice::ReadOnly);
--
1.7.2.5