switched layout to QGraphicsScene

This commit is contained in:
2020-02-20 15:47:32 +01:00
parent 8089203419
commit 452bf5f2e0
8 changed files with 204 additions and 83 deletions
+65 -15
View File
@@ -3,9 +3,10 @@
#include "common.h"
#include <QPageSize>
#include <QPixmap>
#include <QPainter>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QDebug>
@@ -17,6 +18,9 @@ cLayout::cLayout(QWidget *parent) :
{
ui->setupUi(this);
QGraphicsScene* lpScene = new QGraphicsScene;
ui->m_lpLayoutPreview->setScene(lpScene);
for(int id = 0;id < QPageSize::LastPageSize;id++)
ui->m_lpPageSize->addItem(QPageSize::name(static_cast<QPageSize::PageSizeId>(id)), QVariant::fromValue(static_cast<QPageSize::PageSizeId>(id)));
@@ -136,6 +140,51 @@ void cLayout::resizeEvent(QResizeEvent *event)
QWidget::resizeEvent(event);
}
QPageSize cLayout::pageSize()
{
return(QPageSize(ui->m_lpPageSize->currentData().value<QPageSize::PageSizeId>()));
}
qreal cLayout::borderTop()
{
return(ui->m_lpBorderTop->value());
}
qreal cLayout::borderLeft()
{
return(ui->m_lpBorderLeft->value());
}
qreal cLayout::borderRight()
{
return(ui->m_lpBorderRight->value());
}
qreal cLayout::borderBottom()
{
return(ui->m_lpBorderBottom->value());
}
qreal cLayout::gutter()
{
return(ui->m_lpGutterWidth->value());
}
int cLayout::tilesH()
{
return(ui->m_lpTilesH->value());
}
int cLayout::tilesV()
{
return(ui->m_lpTilesV->value());
}
QPageSize::Unit cLayout::unit()
{
return(ui->m_lpUnit->currentData().value<QPageSize::Unit>());
}
void cLayout::redrawPreview()
{
QSize widgetSize = ui->m_lpLayoutPreview->size();
@@ -150,8 +199,8 @@ void cLayout::redrawPreview()
qreal gutter = ui->m_lpGutterWidth->value();
qreal tilesH = static_cast<qreal>(ui->m_lpTilesH->value());
qreal tilesV = static_cast<qreal>(ui->m_lpTilesV->value());
qreal scaleFactor = static_cast<qreal>(widgetSize.width()-10) / pageWidth;
qreal tmp = static_cast<qreal>(widgetSize.height()-10) / pageHeight;
qreal scaleFactor = static_cast<qreal>(widgetSize.width()-40) / pageWidth;
qreal tmp = static_cast<qreal>(widgetSize.height()-40) / pageHeight;
QString unit;
@@ -188,19 +237,19 @@ void cLayout::redrawPreview()
if(tmp < scaleFactor)
scaleFactor = tmp;
QPixmap pixmap(scale(size, scaleFactor));
pixmap.fill(Qt::white);
QPainter painter(&pixmap);
QGraphicsScene* lpScene = ui->m_lpLayoutPreview->scene();
lpScene->clear();
lpScene->setSceneRect(0, 0, scale(size, scaleFactor).width(), scale(size, scaleFactor).height());
QGraphicsRectItem* paperRect = new QGraphicsRectItem(0, 0, scale(size, scaleFactor).width(), scale(size, scaleFactor).height());
paperRect->setBrush(QBrush(Qt::white));
lpScene->addItem(paperRect);
QRectF rect;
qreal rectW = (pageWidth-borderLeft-borderRight-gutter*(tilesH-1)) / tilesH;
qreal rectH = (pageHeight-borderTop-borderBottom-gutter*(tilesV-1)) / tilesV;
// borderTop = scale(borderTop, scaleFactor);
// borderLeft = scale(borderLeft, scaleFactor);
painter.setPen(Qt::black);
for(qreal x = 0;x < tilesH;x++)
{
for(qreal y = 0;y < tilesV;y++)
@@ -210,11 +259,12 @@ void cLayout::redrawPreview()
rect.setTopLeft(scale(QPointF(l, t), scaleFactor));
rect.setSize(scale(QSizeF(rectW, rectH), scaleFactor));
// rect.setTopLeft(QPoint(borderLeft+x*(rectW+gutter), borderTop+y*(rectH+gutter)));
// rect.setSize(QSizeF(rectW, rectH));
painter.drawRect(rect);
QGraphicsRectItem* lpRect = new QGraphicsRectItem(rect);
lpRect->setPen(QPen(Qt::black));
lpScene->addItem(lpRect);
}
}
ui->m_lpLayoutPreview->setPixmap(pixmap);
emit layoutChanged();
}
+21
View File
@@ -8,6 +8,7 @@
#include <QWidget>
#include <QPageSize>
namespace Ui {
@@ -38,6 +39,16 @@ public:
*/
~cLayout();
QPageSize pageSize();
qreal borderTop();
qreal borderLeft();
qreal borderRight();
qreal borderBottom();
qreal gutter();
int tilesH();
int tilesV();
QPageSize::Unit unit();
private:
Ui::cLayout* ui; /*!< TODO: describe */
bool m_initializing; /*!< TODO: describe */
@@ -125,6 +136,16 @@ private slots:
*/
void onGutterChanged(double value);
signals:
/*!
\brief
\fn layoutChanged
\param layout
\param i
*/
void layoutChanged();
protected:
/*!
\brief
+2 -6
View File
@@ -15,7 +15,7 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
@@ -347,11 +347,7 @@
</layout>
</item>
<item>
<widget class="QLabel" name="m_lpLayoutPreview">
<property name="text">
<string/>
</property>
</widget>
<widget class="QGraphicsView" name="m_lpLayoutPreview"/>
</item>
</layout>
</item>
+3 -39
View File
@@ -30,6 +30,8 @@ cMainWindow::cMainWindow(cSplashScreen* lpSplashScreen, QWidget *parent)
createActions();
ui->m_lpMainTab->setCurrentIndex(1);
connect(m_lpLayout, &cLayout::layoutChanged, m_lpPrint, &cPrint::onLayoutChanged);
}
cMainWindow::~cMainWindow()
@@ -87,7 +89,7 @@ void cMainWindow::initUI()
m_lpLayout = new cLayout(this);
ui->m_lpMainTab->addTab(m_lpLayout, "Layout");
m_lpPrint = new cPrint(m_lpProgressBar, ui->m_lpSelectedList, m_lpSelectedListModel, this);
m_lpPrint = new cPrint(m_lpProgressBar, ui->m_lpSelectedList, m_lpSelectedListModel, m_lpLayout, this);
ui->m_lpMainTab->addTab(m_lpPrint, "Print");
if(!settings.value("main/maximized").toBool())
@@ -102,20 +104,10 @@ void cMainWindow::initUI()
if(iX != -1 && iY != -1)
move(iX, iY);
}
// QStringList headerLabels = QStringList() << tr("icon") << tr("path") << tr("file") << tr("size") << tr("date") << tr("width") << tr("height") << ("");
// m_lpFileListModel->setHorizontalHeaderLabels(headerLabels);
}
void cMainWindow::createActions()
{
// setToolButtonStyle(Qt::ToolButtonFollowStyle);
// createFileActions();
// createContextActions();
// connect(ui->m_lpFileList, &cTreeView::deleteEntrys, this, &cMainWindow::onDeleteEntrys);
// connect(ui->m_lpThumbnailSize, &QSlider::valueChanged, this, &cMainWindow::onThumbnailSize);
}
void cMainWindow::createContextActions()
@@ -124,34 +116,6 @@ void cMainWindow::createContextActions()
void cMainWindow::createFileActions()
{
// m_lpFileToolBar = addToolBar("file");
// const QIcon openIcon = QIcon::fromTheme("document-open");
// m_lpOpenFileAction = m_lpFileToolBar->addAction(openIcon, tr("&Open..."), this, &cMainWindow::onAddFile);
// m_lpOpenFileAction->setShortcut(QKeySequence::Open);
// const QIcon openDirectoryIcon = QIcon::fromTheme("folder");
// m_lpOpenDirectoryAction = m_lpFileToolBar->addAction(openIcon, tr("&Open Folder..."), this, &cMainWindow::onAddFolder);
// m_lpListToolBar = addToolBar("list");
// const QIcon deleteIcon = QIcon::fromTheme("edit-delete");
// m_lpDeleteAction = m_lpListToolBar->addAction(deleteIcon, tr("&Delete"), this, &cMainWindow::onDeleteEntrys);
// m_lpDeleteAction->setShortcut(QKeySequence::Delete);
// const QIcon clearIcon = QIcon::fromTheme("edit-clear");
// m_lpClearAction = m_lpListToolBar->addAction(clearIcon, tr("&Clear"), this, &cMainWindow::onClearList);
// m_lpActionToolBar = addToolBar("action");
// const QIcon convertIcon = QIcon::fromTheme("system-run");
// m_lpConvertAction = m_lpActionToolBar->addAction(convertIcon, tr("&Convert"), this, &cMainWindow::onConvert);
// m_lpConvertAction->setShortcut(QKeySequence::Delete);
// const QIcon stopIcon = QIcon::fromTheme("process-stop");
// m_lpStopAction = m_lpActionToolBar->addAction(stopIcon, tr("&Stop"), this, &cMainWindow::onStop);
}
void cMainWindow::setImageFormats()
+31 -2
View File
@@ -20,15 +20,19 @@
#include <QDebug>
cPrint::cPrint(QProgressBar* lpProgressBar, QListView* lpSelectedList, QStandardItemModel* lpSelectedListModel, QWidget *parent) :
cPrint::cPrint(QProgressBar* lpProgressBar, QListView* lpSelectedList, QStandardItemModel* lpSelectedListModel, cLayout* lpLayout, QWidget *parent) :
QWidget(parent),
ui(new Ui::cPrint),
m_lpProgressBar(lpProgressBar),
m_lpSelectedList(lpSelectedList),
m_lpSelectedListModel(lpSelectedListModel)
m_lpSelectedListModel(lpSelectedListModel),
m_lpLayout(lpLayout)
{
ui->setupUi(this);
ui->m_lpPaperSourceLabel->setVisible(false);
ui->m_lpPaperSource->setVisible(false);
m_printerStateText.insert(QPrinter::Idle, tr("idle"));
m_printerStateText.insert(QPrinter::Active, tr("Active"));
m_printerStateText.insert(QPrinter::Aborted, tr("Aborted"));
@@ -72,6 +76,9 @@ cPrint::cPrint(QProgressBar* lpProgressBar, QListView* lpSelectedList, QStandard
ui->m_lpZoomOriginal->setIcon(QIcon::fromTheme("zoom-original"));
ui->m_lpZoomIn->setIcon(QIcon::fromTheme("zoom-in"));
ui->m_lpPrintPreview->setIcon(QIcon::fromTheme("document-print-preview"));
ui->m_lpPrint->setIcon(QIcon::fromTheme("document-print"));
connect(ui->m_lpPrinterSelect, &QComboBox::currentTextChanged, this, &cPrint::onPrinterChanged);
connect(ui->m_lpPrinterProperties, &QPushButton::clicked, this, &cPrint::onPrinterSettings);
connect(ui->m_lpPrintPreview, &QPushButton::clicked, this, &cPrint::onPrintPreview);
@@ -96,6 +103,11 @@ cPrint::~cPrint()
delete ui;
}
void cPrint::onLayoutChanged()
{
ui->m_lpPrintPreview->setEnabled(true);
}
void cPrint::onPrinterChanged(const QString& printer)
{
m_lpPrinter->setPrinterName(printer);
@@ -120,12 +132,16 @@ void cPrint::onPrinterChanged(const QString& printer)
for(int x = 0;x < paperSources.count();x++)
ui->m_lpPaperSource->addItem(m_printerPaperSource[paperSources[x]]);
ui->m_lpPaperSource->setCurrentText(m_printerPaperSource[m_lpPrinter->paperSource()]);
ui->m_lpPrintPreview->setEnabled(true);
}
void cPrint::onPrinterSettings()
{
QPrintDialog printDialog(m_lpPrinter);
printDialog.exec();
ui->m_lpPrintPreview->setEnabled(true);
}
void cPrint::onPrintPreview()
@@ -138,6 +154,8 @@ void cPrint::onPrintPreview()
return;
m_lpPrintPreviewWidget->updatePreview();
ui->m_lpPrintPreview->setEnabled(false);
}
void cPrint::onPaintRequested(QPrinter* printer)
@@ -155,6 +173,17 @@ void cPrint::onPaintRequested(QPrinter* printer)
if(image.isNull())
return;
QPageSize pageSize = m_lpLayout->pageSize();
qreal borderTop = m_lpLayout->borderTop();
qreal borderLeft = m_lpLayout->borderLeft();
qreal borderRight = m_lpLayout->borderRight();
qreal borderBottom = m_lpLayout->borderBottom();
qreal gutter = m_lpLayout->gutter();
int tilesH = m_lpLayout->tilesH();
int tilesV = m_lpLayout->tilesV();
QPageSize::Unit unit = m_lpLayout->unit();
printer->setPageSize(pageSize);
printer->newPage();
QPixmap pixmap = QPixmap::fromImage(image);
+14 -1
View File
@@ -7,6 +7,8 @@
#define CPRINT_H
#include "clayout.h"
#include <QWidget>
#include <QProgressBar>
@@ -37,7 +39,7 @@ public:
\fn cPrint
\param parent
*/
explicit cPrint(QProgressBar* lpProgressBar, QListView* lpSelectedList, QStandardItemModel* lpSelectedListModel, QWidget *parent = nullptr);
explicit cPrint(QProgressBar* lpProgressBar, QListView* lpSelectedList, QStandardItemModel* lpSelectedListModel, cLayout* lpLayout, QWidget *parent = nullptr);
/*!
\brief
@@ -50,6 +52,7 @@ private:
QProgressBar* m_lpProgressBar; /*!< TODO: describe */
QListView* m_lpSelectedList; /*!< TODO: describe */
QStandardItemModel* m_lpSelectedListModel; /*!< TODO: describe */
cLayout* m_lpLayout; /*!< TODO: describe */
QPrinter* m_lpPrinter; /*!< TODO: describe */
QPrintPreviewWidget* m_lpPrintPreviewWidget; /*!< TODO: describe */
@@ -57,7 +60,17 @@ private:
QMap<QPrinter::PrinterState, QString> m_printerStateText; /*!< TODO: describe */
QMap<QPrinter::PaperSource, QString> m_printerPaperSource; /*!< TODO: describe */
public slots:
/*!
\brief
\fn onLayoutChanged
\param layout
*/
void onLayoutChanged();
private slots:
/*!
\brief
+62 -20
View File
@@ -39,7 +39,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_2">
<widget class="QLabel" name="m_lpPrinterSelectLabel">
<property name="text">
<string>Name:</string>
</property>
@@ -86,7 +86,7 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label">
<widget class="QLabel" name="m_lpPrinterMakeAndModelLabel">
<property name="text">
<string>Printer Model:</string>
</property>
@@ -96,7 +96,7 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_4">
<widget class="QLabel" name="m_lpPrinterStatusLabel">
<property name="text">
<string>Status:</string>
</property>
@@ -113,7 +113,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_5">
<widget class="QLabel" name="m_lpPrinterLocationLabel">
<property name="text">
<string>Location:</string>
</property>
@@ -136,7 +136,7 @@
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<widget class="QLabel" name="m_lpPaperSizeLabel">
<property name="text">
<string>Paper Size:</string>
</property>
@@ -162,7 +162,7 @@
<widget class="QComboBox" name="m_lpPaperSize"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_6">
<widget class="QLabel" name="m_lpPaperSourceLabel">
<property name="text">
<string>Paper Source:</string>
</property>
@@ -225,6 +225,12 @@
<property name="text">
<string>zoom to fit</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
@@ -238,6 +244,12 @@
<property name="text">
<string>zoom original</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
@@ -251,6 +263,50 @@
<property name="text">
<string>zoom in</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="m_lpPrintPreview">
<property name="toolTip">
<string>print preview</string>
</property>
<property name="statusTip">
<string>print preview</string>
</property>
<property name="text">
<string>print preview</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="m_lpPrint">
<property name="toolTip">
<string>print</string>
</property>
<property name="statusTip">
<string>print</string>
</property>
<property name="text">
<string>print</string>
</property>
<property name="iconSize">
<size>
<width>24</width>
<height>24</height>
</size>
</property>
</widget>
</item>
<item>
@@ -266,20 +322,6 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="m_lpPrintPreview">
<property name="text">
<string>preview</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_lpPrint">
<property name="text">
<string>print</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
+6
View File
@@ -16,5 +16,11 @@
<file alias="24x24/zoom-in.png">images/arc/24x24/actions/zoom-in.png</file>
<file alias="24x24/zoom-original.png">images/arc/24x24/actions/zoom-original.png</file>
<file alias="24x24/zoom-out.png">images/arc/24x24/actions/zoom-out.png</file>
<file alias="16x16/document-print.png">images/arc/16x16/actions/document-print.png</file>
<file alias="16x16/document-print-preview.png">images/arc/16x16/actions/document-print-preview.png</file>
<file alias="22x22/document-print.png">images/arc/22x22/actions/document-print.png</file>
<file alias="22x22/document-print-preview.png">images/arc/22x22/actions/document-print-preview.png</file>
<file alias="24x24/document-print.png">images/arc/24x24/actions/document-print.png</file>
<file alias="24x24/document-print-preview.png">images/arc/24x24/actions/document-print-preview.png</file>
</qresource>
</RCC>