Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printerselector.w 3.8KB

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