Some print related fixes. I'm trying to fix the thick lines, but without luck till now.
From 7b062c18d4b3d999d6d3d6f5d7b86cbaaa35300a Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 20:10:44 -0300 Subject: [PATCH 1/6] Added a HTML Displayable Delegate
Added a HTML delegate to show hipertext on print. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/modeldelegates.cpp | 29 +++++++++++++++++++++++++++++ qt-ui/modeldelegates.h | 6 ++++++ 2 files changed, 35 insertions(+) diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 930f055..c0b0f4d 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -17,6 +17,7 @@ #include <QKeyEvent> #include <QAbstractItemView> #include <QApplication> +#include <QTextDocument> QSize DiveListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { @@ -399,3 +400,31 @@ QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOption w->setSingleStep(step); return w; } + +void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const +{ + QStyleOptionViewItemV4 options = option; + initStyleOption(&options, index); + painter->save(); + QTextDocument doc; + doc.setHtml(options.text); + doc.setTextWidth(options.rect.width()); + doc.setDefaultFont(options.font); + options.text.clear(); + options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter); + painter->translate(options.rect.left(), options.rect.top()); + QRect clip(0, 0, options.rect.width(), options.rect.height()); + doc.drawContents(painter, clip); + painter->restore(); + ProfilePrintDelegate::paint(painter,option,index); +} + +QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const +{ + QStyleOptionViewItemV4 options = option; + initStyleOption(&options, index); + QTextDocument doc; + doc.setHtml(options.text); + doc.setTextWidth(options.rect.width()); + return QSize(doc.idealWidth(), doc.size().height()); +} \ No newline at end of file diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index bb9854e..88ea3f1 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -110,4 +110,10 @@ private: double step; }; +class HTMLDelegate : public ProfilePrintDelegate { + Q_OBJECT +public: + virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; + virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; +}; #endif // MODELDELEGATES_H -- 2.0.1
From e2f815db69b70789c7e3cf98dadde92473af7230 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 20:18:14 -0300 Subject: [PATCH 2/6] Save / Restore the QPainter before operations. I don't know if this fixes anything, but it is asked of us to do that by the Qt docs. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/modeldelegates.cpp | 2 ++ qt-ui/profile/diveprofileitem.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index c0b0f4d..b11c461 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -353,6 +353,7 @@ void ProfilePrintDelegate::paint(QPainter *painter, const QStyleOptionViewItem & const int row = index.row(); const int col = index.column(); + painter->save(); // grid color painter->setPen(QPen(QColor(0xff999999))); // horizontal lines @@ -366,6 +367,7 @@ void ProfilePrintDelegate::paint(QPainter *painter, const QStyleOptionViewItem & if (col == 4 || (col == 0 && row > 5)) painter->drawLine(rect.topRight(), rect.bottomRight()); } + painter->restore(); QStyledItemDelegate::paint(painter, option, index); } diff --git a/qt-ui/profile/diveprofileitem.cpp b/qt-ui/profile/diveprofileitem.cpp index 1c8a13f..cb5cec5 100644 --- a/qt-ui/profile/diveprofileitem.cpp +++ b/qt-ui/profile/diveprofileitem.cpp @@ -120,6 +120,7 @@ void DiveProfileItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o if (polygon().isEmpty()) return; + painter->save(); // This paints the Polygon + Background. I'm setting the pen to QPen() so we don't get a black line here, // after all we need to plot the correct velocities colors later. setPen(Qt::NoPen); @@ -138,6 +139,7 @@ void DiveProfileItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->setPen(pen); painter->drawLine(poly[i - 1], poly[i]); } + painter->restore(); } int DiveProfileItem::maxCeiling(int row) @@ -325,8 +327,10 @@ void DiveHeartrateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem { if (polygon().isEmpty()) return; + painter->save(); painter->setPen(pen()); painter->drawPolyline(polygon()); + painter->restore(); } void DiveHeartrateItem::settingsChanged() @@ -421,8 +425,10 @@ void DiveTemperatureItem::paint(QPainter *painter, const QStyleOptionGraphicsIte { if (polygon().isEmpty()) return; + painter->save(); painter->setPen(pen()); painter->drawPolyline(polygon()); + painter->restore(); } void DiveGasPressureItem::modelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) @@ -516,6 +522,7 @@ void DiveGasPressureItem::paint(QPainter *painter, const QStyleOptionGraphicsIte QPen pen; pen.setCosmetic(true); pen.setWidth(2); + painter->save(); struct plot_data *entry = dataModel->data().entry; Q_FOREACH (const QPolygonF &poly, polygons) { for (int i = 1, count = poly.count(); i < count; i++, entry++) { @@ -524,6 +531,7 @@ void DiveGasPressureItem::paint(QPainter *painter, const QStyleOptionGraphicsIte painter->drawLine(poly[i - 1], poly[i]); } } + painter->restore(); } DiveCalculatedCeiling::DiveCalculatedCeiling() : is3mIncrement(false), gradientFactor(new DiveTextItem(this)) @@ -711,6 +719,7 @@ void PartialPressureGasItem::modelDataChanged(const QModelIndex &topLeft, const void PartialPressureGasItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { const qreal pWidth = 0.0; + painter->save(); painter->setPen(QPen(normalColor, pWidth)); painter->drawPolyline(polygon()); @@ -718,6 +727,7 @@ void PartialPressureGasItem::paint(QPainter *painter, const QStyleOptionGraphics painter->setPen(QPen(alertColor, pWidth)); Q_FOREACH (const QPolygonF &poly, alertPolygons) painter->drawPolyline(poly); + painter->restore(); } void PartialPressureGasItem::setThreshouldSettingsKey(const QString &threshouldSettingsKey) -- 2.0.1
From b60364e98e30a132e60fb4801479ffedc3bda507 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 20:27:18 -0300 Subject: [PATCH 3/6] No need to delete a QPointer QPointer is a smart pointer, it will delete itself when the refcount == 0. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/printlayout.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/qt-ui/printlayout.cpp b/qt-ui/printlayout.cpp index 6640d43..51b44c0 100644 --- a/qt-ui/printlayout.cpp +++ b/qt-ui/printlayout.cpp @@ -213,7 +213,6 @@ void PrintLayout::printProfileDives(int divesPerRow, int divesPerColumn) } // cleanup painter.end(); - delete table; profile->setFrameStyle(profileFrameStyle); profile->setPrintMode(false); profile->resize(originalSize); -- 2.0.1
From ebf18acc1970c4761c425f06b414286296877278 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 20:34:11 -0300 Subject: [PATCH 4/6] Fixed memleak The model was not being deleted when the table was, and thus we recreated it for every print. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/printlayout.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qt-ui/printlayout.cpp b/qt-ui/printlayout.cpp index 51b44c0..8b4d4ab 100644 --- a/qt-ui/printlayout.cpp +++ b/qt-ui/printlayout.cpp @@ -283,7 +283,7 @@ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int // resize table->resize(tableW, profilePrintTableMaxH); // hide the grid and set a stylesheet - table->setItemDelegate(new ProfilePrintDelegate(this)); + table->setItemDelegate(new ProfilePrintDelegate(table)); table->setShowGrid(false); table->setStyleSheet( "QTableView { border: none }" -- 2.0.1
From 7191f41275bd0b23bf61bb8e13bd201e798bb1b0 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 21:06:12 -0300 Subject: [PATCH 5/6] Add the HTML Delegate to printing. This patch just adds the HTML Delegate to print the text. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/modeldelegates.cpp | 4 ++++ qt-ui/modeldelegates.h | 1 + qt-ui/printlayout.cpp | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index b11c461..3fb8bea 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -403,6 +403,10 @@ QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOption return w; } +HTMLDelegate::HTMLDelegate(QObject *parent) : ProfilePrintDelegate(parent) +{ +} + void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const { QStyleOptionViewItemV4 options = option; diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h index 88ea3f1..bc5dd3f 100644 --- a/qt-ui/modeldelegates.h +++ b/qt-ui/modeldelegates.h @@ -113,6 +113,7 @@ private: class HTMLDelegate : public ProfilePrintDelegate { Q_OBJECT public: + explicit HTMLDelegate(QObject *parent = 0); virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; }; diff --git a/qt-ui/printlayout.cpp b/qt-ui/printlayout.cpp index 8b4d4ab..384a0ef 100644 --- a/qt-ui/printlayout.cpp +++ b/qt-ui/printlayout.cpp @@ -261,7 +261,6 @@ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int // notes table->setSpan(6, 0, 1, 5); table->setSpan(7, 0, 5, 5); - /* resize row heights to the 'profilePrintRowHeights' indexes. * profilePrintTableMaxH will then hold the table height. */ int i; @@ -284,6 +283,8 @@ QTableView *PrintLayout::createProfileTable(ProfilePrintModel *model, const int table->resize(tableW, profilePrintTableMaxH); // hide the grid and set a stylesheet table->setItemDelegate(new ProfilePrintDelegate(table)); + table->setItemDelegateForRow(7, new HTMLDelegate(table)); + table->setShowGrid(false); table->setStyleSheet( "QTableView { border: none }" -- 2.0.1
From f2239b9ec8a1f4e3c4c9b9ae02505b36860a7c01 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 17 Jul 2014 21:25:57 -0300 Subject: [PATCH 6/6] Close the print dialog after a sucessfull print Don't know if this will be accepted, but I think it's intuitive to do not warn if everything was according to the plan, and keep the dialog open after a print was due is something that I find it strange. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/printdialog.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/qt-ui/printdialog.cpp b/qt-ui/printdialog.cpp index 488afb5..8f1a5ca 100644 --- a/qt-ui/printdialog.cpp +++ b/qt-ui/printdialog.cpp @@ -75,8 +75,10 @@ void PrintDialog::previewClicked(void) void PrintDialog::printClicked(void) { QPrintDialog printDialog(&printer, this); - if (printDialog.exec() == QDialog::Accepted) + if (printDialog.exec() == QDialog::Accepted){ printLayout->print(); + close(); + } } void PrintDialog::onPaintRequested(QPrinter *printerPtr) -- 2.0.1
_______________________________________________ subsurface mailing list [email protected] http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface
