]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QPrint.C
some more random changes, added Timeout (make clean if LyX crashes !!)
[lyx.git] / src / frontends / qt2 / QPrint.C
1 /**
2  * \file QPrint.C
3  * Copyright 2001 LyX Team
4  * see the file COPYING
5  *
6  * \author John Levon, moz@compsoc.man.ac.uk
7  */
8
9 #include <config.h>
10
11 #include "QPrintDialog.h"
12 #include "QPrint.h"
13 #include "Dialogs.h"
14 #include "gettext.h"
15 #include "buffer.h"
16 #include "lyxrc.h" 
17 #include "QtLyXView.h" 
18
19 #include "PrinterParams.h" 
20 #include "Liason.h" 
21 #include "BufferView.h" 
22 #include "support/lstrings.h"
23 #include "qmessagebox.h"
24
25 using Liason::printBuffer;
26 using Liason::getPrinterParams;
27 using std::max;
28
29 QPrint::QPrint(LyXView *v, Dialogs *d)
30         : dialog_(0), lv_(v), d_(d), h_(0), u_(0)
31 {
32         d->showPrint.connect(SigC::slot(this, &QPrint::show));
33 }
34
35
36 QPrint::~QPrint()
37 {
38         delete dialog_;
39 }
40
41
42 // we can safely ignore the parameter because we can always update
43 void QPrint::update(bool)
44 {
45         if (!lv_->view()->available())
46                return;
47
48         PrinterParams pp(getPrinterParams(lv_->buffer()));
49
50         dialog_->setTarget(pp.target);
51         dialog_->setPrinter(pp.printer_name.c_str());
52         dialog_->setFile(pp.file_name.c_str());
53         dialog_->setWhichPages(pp.which_pages);
54         dialog_->setReverse(pp.reverse_order);
55         dialog_->setSort(pp.unsorted_copies);
56         dialog_->setCount(pp.count_copies);
57         
58         if (!pp.from_page.empty()) {
59                 dialog_->setFrom(pp.from_page.c_str());
60                 if (pp.to_page)
61                         dialog_->setTo(tostr(pp.to_page).c_str());
62                 else
63                         dialog_->setTo("");
64         } else {
65                 dialog_->setFrom("");
66                 dialog_->setTo("");
67         }
68 }
69
70  
71 void QPrint::print()
72 {
73         if (!lv_->view()->available())
74                return;
75
76         string from;
77         int to(0);
78
79         if (strlen(dialog_->getFrom())) {
80                 from = dialog_->getFrom();
81                 if (strlen(dialog_->getTo()))
82                         to = strToInt(dialog_->getTo());
83         }
84         
85         int retval = printBuffer(lv_->buffer(), PrinterParams(dialog_->getTarget(),
86                 string(dialog_->getPrinter()), string(dialog_->getFile()), 
87                 dialog_->getWhichPages(), from, to, dialog_->getReverse(), 
88                 dialog_->getSort(), max(strToInt(dialog_->getCount()),1)));
89
90         if (!retval) {
91                 // FIXME: should have a utility class for this
92                 string message(_("An error occured while printing.\n\n"));
93                 message += _("Check the parameters are correct.\n");
94                 QMessageBox msg( _("LyX: Print Error"), message.c_str(), QMessageBox::Warning, 1,0,0);
95                 msg.raise();
96                 msg.setActiveWindow();
97                 msg.show();
98         }
99 }
100
101
102 void QPrint::show()
103 {
104         if (!dialog_)
105                 dialog_ = new QPrintDialog(this, 0, _("LyX: Print"));
106  
107         if (!dialog_->isVisible()) {
108                 h_ = d_->hideBufferDependent.connect(SigC::slot(this, &QPrint::hide));
109                 u_ = d_->updateBufferDependent.connect(SigC::slot(this, &QPrint::update));
110         }
111
112         dialog_->raise();
113         dialog_->setActiveWindow();
114  
115         update();
116         dialog_->show();
117 }
118
119
120 void QPrint::close()
121 {
122         h_.disconnect();
123         u_.disconnect();
124 }
125
126  
127 void QPrint::hide()
128 {
129         dialog_->hide();
130         close();
131 }