Allow to select printer in Layout Settings dialog.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2016-08-22 19:43:22 +03:00
parent 73bcc89107
commit e54e1e4f11
9 changed files with 231 additions and 96 deletions

View File

@ -35,6 +35,7 @@
#include <QMessageBox>
#include <QPushButton>
#include <QPrinterInfo>
//must be the same order as PaperSizeTemplate constants
const DialogLayoutSettings::FormatsVector DialogLayoutSettings::pageFormatNames =
@ -68,6 +69,7 @@ DialogLayoutSettings::DialogLayoutSettings(VLayoutGenerator *generator, QWidget
InitTemplates();
MinimumPaperSize();
MinimumLayoutSize();
InitPrinter();
//in export console mode going to use defaults
if (disableSettings == false)
@ -79,24 +81,27 @@ DialogLayoutSettings::DialogLayoutSettings(VLayoutGenerator *generator, QWidget
RestoreDefaults();
}
connect(ui->comboBoxTemplates, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
connect(ui->comboBoxPrinter, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &DialogLayoutSettings::PrinterMargins);
connect(ui->comboBoxTemplates, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &DialogLayoutSettings::TemplateSelected);
connect(ui->comboBoxPaperSizeUnit, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
connect(ui->comboBoxPaperSizeUnit, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &DialogLayoutSettings::ConvertPaperSize);
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::PaperSizeChanged);
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::PaperSizeChanged);
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::FindTemplate);
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::FindTemplate);
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperWidth, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::CorrectMaxFileds);
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
connect(ui->doubleSpinBoxPaperHeight, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
this, &DialogLayoutSettings::CorrectMaxFileds);
connect(ui->checkBoxIgnoreFileds, &QCheckBox::stateChanged, this, &DialogLayoutSettings::IgnoreAllFields);
@ -333,6 +338,12 @@ void DialogLayoutSettings::SetIgnoreAllFields(bool value)
ui->checkBoxIgnoreFileds->setChecked(value);
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogLayoutSettings::SelectedPrinter() const
{
return ui->comboBoxPrinter->currentText();
}
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutSettings::TemplateSelected()
{
@ -617,6 +628,10 @@ void DialogLayoutSettings::RestoreDefaults()
TemplateSelected();
ui->comboBoxTemplates->blockSignals(false);
ui->comboBoxPrinter->blockSignals(true);
InitPrinter();
ui->comboBoxPrinter->blockSignals(false);
SetLayoutWidth(VSettings::GetDefLayoutWidth());
SetShift(VSettings::GetDefLayoutShift());
SetGroup(VSettings::GetDefLayoutGroup());
@ -630,6 +645,16 @@ void DialogLayoutSettings::RestoreDefaults()
IgnoreAllFields(ui->checkBoxIgnoreFileds->isChecked());
}
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutSettings::PrinterMargins()
{
QPrinterInfo printer = QPrinterInfo::printerInfo(ui->comboBoxPrinter->currentText());
if (not printer.isNull())
{
SetFields(VSettings::GetPrinterFields(QSharedPointer<QPrinter>(new QPrinter(printer))));
}
}
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutSettings::CorrectMaxFileds()
{
@ -714,6 +739,51 @@ void DialogLayoutSettings::InitTemplates()
ui->comboBoxTemplates->setCurrentIndex(-1);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutSettings::InitPrinter()
{
ui->comboBoxPrinter->clear();
QStringList printerNames;
#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
const QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
for(int i = 0; i < printers.size(); ++i)
{
const QString name = printers.at(i).printerName();
if (not name.isEmpty())
{
printerNames.append(name);
}
}
#else
printerNames = QPrinterInfo::availablePrinterNames();
#endif
ui->comboBoxPrinter->addItems(printerNames);
if (ui->comboBoxPrinter->count() == 0)
{
ui->comboBoxPrinter->addItem(tr("None", "Printer"));
}
else
{
QString defPrinterName;
#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
const QPrinterInfo def = QPrinterInfo::defaultPrinter();
if(not def.isNull())
{
defPrinterName = def.printerName();
}
#else
defPrinterName = QPrinterInfo::defaultPrinterName();
#endif
const int index = ui->comboBoxPrinter->findText(defPrinterName);
if(index != -1)
{
ui->comboBoxPrinter->setCurrentIndex(index);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
QString DialogLayoutSettings::MakeHelpTemplateList()
{

View File

@ -111,6 +111,8 @@ public:
bool IsIgnoreAllFields() const;
void SetIgnoreAllFields(bool value);
QString SelectedPrinter() const;
//support functions for the command line parser which uses invisible dialog to properly build layout generator
bool SelectTemplate(const PaperSizeTemplate& id);
static QString MakeHelpTemplateList();
@ -131,6 +133,7 @@ private slots:
void PaperSizeChanged();
void Swap(bool checked);
void RestoreDefaults();
void PrinterMargins();
void CorrectMaxFileds();
void IgnoreAllFields(int state);
@ -151,6 +154,7 @@ private:
void InitPaperUnits();
void InitLayoutUnits();
void InitTemplates();
void InitPrinter();
QSizeF Template();
QSizeF TemplateSize(const PaperSizeTemplate &tmpl) const;
QSizeF RoundTemplateSize(qreal width, qreal height) const;

View File

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>632</width>
<width>600</width>
<height>531</height>
</rect>
</property>
@ -72,64 +72,6 @@
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="labelWidth">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Width:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelHeight">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Height:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxPaperHeight">
<property name="minimumSize">
<size>
<width>94</width>
<height>0</height>
</size>
</property>
<property name="decimals">
<number>2</number>
</property>
<property name="maximum">
<double>99999.000000000000000</double>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxPaperWidth">
<property name="minimumSize">
<size>
<width>94</width>
<height>0</height>
</size>
</property>
<property name="decimals">
<number>2</number>
</property>
<property name="maximum">
<double>99999.990000000005239</double>
</property>
</widget>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
@ -180,6 +122,32 @@
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labelWidth">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Width:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelHeight">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Height:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboBoxPaperSizeUnit">
<property name="sizePolicy">
@ -196,13 +164,65 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxPaperHeight">
<property name="minimumSize">
<size>
<width>94</width>
<height>0</height>
</size>
</property>
<property name="decimals">
<number>2</number>
</property>
<property name="maximum">
<double>99999.000000000000000</double>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxPaperWidth">
<property name="minimumSize">
<size>
<width>94</width>
<height>0</height>
</size>
</property>
<property name="decimals">
<number>2</number>
</property>
<property name="maximum">
<double>99999.990000000005239</double>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Printer:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxPrinter"/>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBoxPaperFileds">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Fields</string>
</property>
@ -210,72 +230,78 @@
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<item row="1" column="0">
<widget class="QLabel" name="labelLeftField">
<property name="text">
<string>Left:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxLeftField">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="labelRightField">
<property name="text">
<string>Right:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxRightField">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="labelTopField">
<property name="text">
<string>Top:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxTopField">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="labelBottomField">
<property name="text">
<string>Bottom:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBoxBottomField">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="checkBoxIgnoreFileds">
<property name="text">
<string>Ignore fileds</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxIgnoreFileds">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Ignore fileds</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
@ -700,7 +726,7 @@
</connection>
</connections>
<buttongroups>
<buttongroup name="buttonGroup"/>
<buttongroup name="buttonGroupPrinciple"/>
<buttongroup name="buttonGroup"/>
</buttongroups>
</ui>

View File

@ -69,7 +69,8 @@ MainWindowsNoGUI::MainWindowsNoGUI(QWidget *parent)
paperSize(),
isTiled(false),
isAutoCrop(false),
isUnitePages(false)
isUnitePages(false),
layoutPrinterName()
{
InitTempLayoutScene();
@ -98,6 +99,7 @@ void MainWindowsNoGUI::ToolLayoutSettings(bool checked)
tButton->setChecked(false);
return;
}
layoutPrinterName = layout.SelectedPrinter();
LayoutSettings(lGenerator);
tButton->setChecked(false);
}
@ -854,7 +856,12 @@ void MainWindowsNoGUI::PrintPreview()
}
}
QSharedPointer<QPrinter> printer = DefaultPrinter();
QPrinterInfo info = QPrinterInfo::printerInfo(layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
@ -881,7 +888,12 @@ void MainWindowsNoGUI::LayoutPrint()
}
}
// display print dialog and if accepted print
QSharedPointer<QPrinter> printer = DefaultPrinter(QPrinter::HighResolution);
QPrinterInfo info = QPrinterInfo::printerInfo(layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info, QPrinter::HighResolution);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),

View File

@ -109,6 +109,8 @@ private:
bool isAutoCrop;
bool isUnitePages;
QString layoutPrinterName;
void CreateShadows();
void CreateScenes();

View File

@ -1813,24 +1813,35 @@ QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath)
}
//---------------------------------------------------------------------------------------------------------------------
QSharedPointer<QPrinter> DefaultPrinter(QPrinter::PrinterMode mode)
QSharedPointer<QPrinter> PreparePrinter(const QPrinterInfo &info, QPrinter::PrinterMode mode)
{
QPrinterInfo def = QPrinterInfo::defaultPrinter();
//if there is no default printer set the print preview won't show
if(def.isNull() || def.printerName().isEmpty())
QPrinterInfo tmpInfo = info;
if(tmpInfo.isNull() || tmpInfo.printerName().isEmpty())
{
if(QPrinterInfo::availablePrinters().isEmpty())
#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
const QList<QPrinterInfo> list = QPrinterInfo::availablePrinters();
if(list.isEmpty())
{
return QSharedPointer<QPrinter>();
}
else
{
def = QPrinterInfo::availablePrinters().first();
tmpInfo = list.first();
}
#else
const QStringList list = QPrinterInfo::availablePrinterNames();
if(list.isEmpty())
{
return QSharedPointer<QPrinter>();
}
else
{
tmpInfo = QPrinterInfo::printerInfo(list.first());
}
#endif
}
auto printer = QSharedPointer<QPrinter>(new QPrinter(def, mode));
auto printer = QSharedPointer<QPrinter>(new QPrinter(tmpInfo, mode));
printer->setResolution(static_cast<int>(PrintDPI));
return printer;
}

View File

@ -620,7 +620,8 @@ QString StrippedName(const QString &fullFileName) Q_REQUIRED_RESULT;
QString RelativeMPath(const QString &patternPath, const QString &absoluteMPath) Q_REQUIRED_RESULT;
QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath) Q_REQUIRED_RESULT;
QSharedPointer<QPrinter> DefaultPrinter(QPrinter::PrinterMode mode = QPrinter::ScreenResolution) Q_REQUIRED_RESULT;
QSharedPointer<QPrinter> PreparePrinter(const QPrinterInfo &info,
QPrinter::PrinterMode mode = QPrinter::ScreenResolution) Q_REQUIRED_RESULT;
QPixmap darkenPixmap(const QPixmap &pixmap) Q_REQUIRED_RESULT;

View File

@ -37,6 +37,7 @@
#include <QStringData>
#include <QStringDataPtr>
#include <QVariant>
#include <QPrinterInfo>
#include "../vmisc/def.h"
#include "../vmisc/vmath.h"
@ -375,7 +376,12 @@ QMarginsF VSettings::GetFields() const
//---------------------------------------------------------------------------------------------------------------------
QMarginsF VSettings::GetDefFields()
{
QSharedPointer<QPrinter> printer = DefaultPrinter();
return GetPrinterFields(PreparePrinter(QPrinterInfo::defaultPrinter()));
}
//---------------------------------------------------------------------------------------------------------------------
QMarginsF VSettings::GetPrinterFields(const QSharedPointer<QPrinter> &printer)
{
if (printer.isNull())
{
return QMarginsF();

View File

@ -39,6 +39,8 @@
#include "vcommonsettings.h"
class QMarginsF;
class QPrinter;
template <class T> class QSharedPointer;
#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
# include "../vmisc/backport/qmarginsf.h"
@ -112,6 +114,7 @@ public:
QMarginsF GetFields() const;
static QMarginsF GetDefFields();
static QMarginsF GetPrinterFields(const QSharedPointer<QPrinter> &printer);
void SetFields(const QMarginsF &value);
Cases GetLayoutGroup() const;