Browse Source

Allow printing directly to a non-default printer and convenience selector.

Neal Wilson 7 years ago
parent
commit
e8a8948115
4 changed files with 114 additions and 13 deletions
  1. 4
    2
      src/Typica.pro
  2. 87
    0
      src/printerselector.w
  3. 2
    0
      src/typica.w
  4. 21
    11
      src/webview.w

+ 4
- 2
src/Typica.pro View File

@@ -26,7 +26,8 @@ HEADERS += moc_typica.cpp \
26 26
     scale.h \
27 27
     draglabel.h \
28 28
     daterangeselector.h \
29
-    licensewindow.h
29
+    licensewindow.h \
30
+    printerselector.h
30 31
 SOURCES += typica.cpp \
31 32
     helpmenu.cpp \
32 33
     abouttypica.cpp \
@@ -36,7 +37,8 @@ SOURCES += typica.cpp \
36 37
     scale.cpp \
37 38
     draglabel.cpp \
38 39
     daterangeselector.cpp \
39
-    licensewindow.cpp
40
+    licensewindow.cpp \
41
+    printerselector.cpp
40 42
 
41 43
 RESOURCES += \
42 44
     resources.qrc

+ 87
- 0
src/printerselector.w View File

@@ -0,0 +1,87 @@
1
+@* Saved Printers.
2
+
3
+\noindent In most cases it's best to handle printing in a way that is common
4
+across many applications. Put a Print menu option in a File menu, bring up the
5
+platform's standard print dialog, and allow people to take full advantage of
6
+the flexibility this provides.
7
+
8
+In more specialized use cases, however, it may make more sense to provide
9
+faster access to a printer that might not be the default printer for that
10
+computer. The first use in Typica where this makes sense is in printing tags
11
+that can follow the coffee and uniquely identify that batch. Using a full sheet
12
+of paper for this might be excessive and time consuming. Instead, it might make
13
+sense to get a small, inexpensive thermal receipt printer to keep at the
14
+roaster. If this were not the default printer, it would quickly become tedious
15
+to bring up the print dialog and change the selected printer after every batch.
16
+
17
+In cases like this, it would be better to provide a combo box in the window
18
+where a printer can be selected and remembered as the default printer just for
19
+that particular use, and allowing people to print directly to that printer
20
+without going through extra steps.
21
+
22
+@(printerselector.h@>=
23
+#include <QPrinterInfo>
24
+#include <QComboBox>
25
+
26
+#ifndef TypicaPrinterSelectorHeader
27
+#define TypicaPrinterSelectorHeader
28
+
29
+class PrinterSelector : public QComboBox@/
30
+{
31
+	@[Q_OBJECT@]@;
32
+	public:
33
+		PrinterSelector();
34
+};
35
+
36
+#endif
37
+
38
+@ The main file also requires this header.
39
+
40
+@<Header files to include@>=
41
+#include "printerselector.h"
42
+
43
+@ Implementation of this class is in a separate file.
44
+
45
+@(printerselector.cpp@>=
46
+#include "printerselector.h"
47
+
48
+@<PrinterSelector implementation@>@;
49
+
50
+@ The constructor looks at the list of available printers and populates itself
51
+with these.
52
+
53
+@<PrinterSelector implementation@>=
54
+PrinterSelector::PrinterSelector() : QComboBox(NULL)
55
+{
56
+	QList<QPrinterInfo> printers = QPrinterInfo::availablePrinters();
57
+	foreach(QPrinterInfo info, printers)
58
+	{
59
+		addItem(info.printerName());
60
+	}
61
+}
62
+
63
+@ The host environment is informed of this class in the usual way starting with
64
+a constructor function prototype.
65
+
66
+@<Function prototypes for scripting@>=
67
+QScriptValue constructPrinterSelector(QScriptContext *context,
68
+                                      QScriptEngine *engine);
69
+
70
+@ The engine is informed of this function.
71
+
72
+@<Set up the scripting engine@>=
73
+constructor = engine->newFunction(constructPrinterSelector);
74
+engine->globalObject().setProperty("PrinterSelector", constructor);
75
+
76
+@ There is nothing special about the constructor. If there were additional
77
+properties needed beyond those supplied by |setQComboBoxProperties()| it would
78
+make sense to add another function to the chain for setting script value
79
+properties.
80
+
81
+@<Functions for scripting@>=
82
+QScriptValue constructPrinterSelector(QScriptContext *, QScriptEngine *engine)
83
+{
84
+	QScriptValue object = engine->newQObject(new PrinterSelector);
85
+	setQComboBoxProperties(object, engine);
86
+	return object;
87
+}

+ 2
- 0
src/typica.w View File

@@ -12752,6 +12752,8 @@ void CSVOutput::setDevice(QIODevice *device)
12752 12752
 
12753 12753
 @i webview.w
12754 12754
 
12755
+@i printerselector.w
12756
+
12755 12757
 @* The Application class.
12756 12758
 
12757 12759
 The |Application| class represents the \pn{} program. It is responsible for

+ 21
- 11
src/webview.w View File

@@ -31,7 +31,7 @@ class TypicaWebView : public QWebView@/
31 31
 	public:@/
32 32
 		TypicaWebView();
33 33
 		@[Q_INVOKABLE@,@, void@]@, load(const QString &url);@t\2\2@>@/
34
-		@[Q_INVOKABLE@,@, void@]@, print();@t\2\2@>@/
34
+		@[Q_INVOKABLE@,@, void@]@, print(const QString &printerName = QString());@t\2\2@>@/
35 35
 		@[Q_INVOKABLE@,@, void@]@, setHtml(const QString &html, const QUrl &baseUrl = QUrl());@t\2\2@>@/
36 36
 		@[Q_INVOKABLE@,@, void@]@, setContent(QIODevice *device);@t\2\2@>@/
37 37
 		@[Q_INVOKABLE@,@, QString@]@, saveXml();@t\2\2@>@/
@@ -115,16 +115,6 @@ void TypicaWebView::load(const QString &url)
115 115
 	QWebView::load(QUrl(url));
116 116
 }
117 117
 
118
-void TypicaWebView::print()
119
-{
120
-	QPrinter *printer = new QPrinter(QPrinter::HighResolution);
121
-	QPrintDialog printDialog(printer, NULL);
122
-	if(printDialog.exec() == QDialog::Accepted)
123
-	{
124
-		QWebView::print(printer);
125
-	}
126
-}
127
-
128 118
 void TypicaWebView::setHtml(const QString &html, const QUrl &baseUrl)
129 119
 {
130 120
 	QWebView::setHtml(html, baseUrl);
@@ -144,6 +134,26 @@ QString TypicaWebView::saveXml()
144 134
 	return page()->currentFrame()->documentElement().toOuterXml();
145 135
 }
146 136
 
137
+@ Print functionality has been extended to allow an optional argument. If the
138
+name of a printer is passed in, the print dialog will be bypassed.
139
+
140
+@<TypicaWebView implementation@>=
141
+void TypicaWebView::print(const QString &printerName)
142
+{
143
+	QPrinter *printer = new QPrinter(QPrinter::HighResolution);
144
+	if(!printerName.isEmpty())
145
+	{
146
+		printer->setPrinterName(printerName);
147
+		QWebView::print(printer);
148
+		return;
149
+	}
150
+	QPrintDialog printDialog(printer, NULL);
151
+	if(printDialog.exec() == QDialog::Accepted)
152
+	{
153
+		QWebView::print(printer);
154
+	}
155
+}
156
+
147 157
 @ Web views are exposed to the host environment in the usual manner.
148 158
 
149 159
 @<Set up the scripting engine@>=

Loading…
Cancel
Save