summaryrefslogtreecommitdiffstats
path: root/src/experimental/qprinterlistview.cpp
blob: 6323c1a7685d23e2d1b1de3afdeb1c25f62dec92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/****************************************************************************
**
** Copyright (C) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Itemviews NG project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qprinterlistview.h"
#include "qprinterlistview_p.h"

#ifndef QT_NO_PRINTER

#include "../qlistcontroller.h"
#include "../qlistmodelinterface.h"
#include "../qgraphicslistview.h"

#include <qstyleoption.h>
#include <qpainter.h>
#include <qtextdocument.h>
#include <qabstracttextdocumentlayout.h>
#include <qdebug.h>

QT_BEGIN_NAMESPACE

QtPrinterListViewPrivate::QtPrinterListViewPrivate(QPrinter *p)
    : q_ptr(0),
    ownsPrinter(p == 0),
    printer(ownsPrinter ? new QPrinter : p),
    model(0),
    orientation(Qt::Vertical),
    header(0),
    footer(0),
    creator(0)
{
    headerFooterSpacing = 5; // in mm
}

QtPrinterListViewPrivate::~QtPrinterListViewPrivate()
{
    delete header;
    delete footer;
    delete creator;
    if (ownsPrinter)
        delete printer;
}

qreal QtPrinterListViewPrivate::printHeader(QPainter &painter)
{
    if (header == 0)
        return 0;
    header->drawContents(&painter);
    return header->size().height() + headerFooterSpacingInPixels();
}

qreal QtPrinterListViewPrivate::printFooter(QPainter &painter)
{
    if (footer == 0)
        return 0;
    qreal pageHeight = printer->pageRect().height();
    const qreal footerSize = footer->size().height();
    painter.translate(0, pageHeight - footerSize);
    footer->drawContents(&painter);
    painter.translate(0, - (pageHeight - footerSize));
    return footerSize + headerFooterSpacingInPixels();
}

QTextDocument *QtPrinterListViewPrivate::getOrCreateHeader() const
{
    if (header == 0) {
        header = new QTextDocument();
        header->setUndoRedoEnabled(false);
        header->setDocumentMargin(0);
    }
    return header;
}

QTextDocument *QtPrinterListViewPrivate::getOrCreateFooter() const
{
    if (footer == 0) {
        footer = new QTextDocument();
        footer->setUndoRedoEnabled(false);
        footer->setDocumentMargin(0);
    }
    return footer;
}

/*!
  \class QtPrinterListView
  \brief QtPrinterListView allows you to print a list model.

  Usage;
  \code
  QtPrinterListView printerView(myListModel);
  QPrintDialog dialog(printerView.printer());
  if (dialog.exec() == QPrintDialog::Accepted) {
    printerView.print();
  }
  \endcode
*/

/*!
  This is an overloaded member function, provided for convenience. It
  differs from the above function only in what argument(s) it accepts. This
  will cause QtPrinterListView to create an internal, default constructed
  QPrinter object, which will be used to print the list.
*/
QtPrinterListView::QtPrinterListView(QtListModelInterface *model)
    : d_ptr(new QtPrinterListViewPrivate(0))
{
    Q_ASSERT(model);
    d_ptr->q_ptr = this;
    d_ptr->model = model;
}

/*!
  Constructs a QtPrinterListView based on printer and with \a model as the
  model that will be printed
*/
QtPrinterListView::QtPrinterListView(QPrinter *printer, QtListModelInterface *model)
    : d_ptr(new QtPrinterListViewPrivate(printer))
{
    Q_ASSERT(model);
    d_ptr->q_ptr = this;
    d_ptr->model = model;
}

/*!
  Destructor
*/
QtPrinterListView::~QtPrinterListView()
{
    delete d_ptr;
}

/*!
  Returns the printer used by this view to print to to allow setting of printer properties.
*/
QPrinter *QtPrinterListView::printer()
{
    Q_D(QtPrinterListView);
    return d->printer;
}

/*!
  Sets the orientation of the list layout to be the given orientation.
  The orientation defines the print direction of the list.
*/
void QtPrinterListView::setOrientation(Qt::Orientation orientation)
{
    Q_D(QtPrinterListView);
    d->orientation = orientation;
}

/*!
  Returns the orientation for this list.
  The orientation defines the print direction of the list.
 */
Qt::Orientation QtPrinterListView::orientation() const
{
    Q_D(const QtPrinterListView);
    return d->orientation;
}

/*!
  Returns the text document used for each page header.
  \sa setHeaderText()
*/
QTextDocument *QtPrinterListView::headerDocument()
{
    Q_D(QtPrinterListView);
    return d->getOrCreateHeader();
}

/*!
  Returns the text document used for each page footer.
  \sa setFooterText()
*/
QTextDocument *QtPrinterListView::footerDocument()
{
    Q_D(QtPrinterListView);
    return d->getOrCreateFooter();
}

/*!
  Sets the header on all pages to have the \a header text.
  This replaces any content that the header had previously.
  \sa headerDocument()
*/
void QtPrinterListView::setHeaderText(const QString &header)
{
    Q_D(QtPrinterListView);
    d->getOrCreateHeader()->setPlainText(header);
}

/*!
  Sets the footer on all pages to have the \a footer text.
  This replaces any content that the footer had previously.
  \sa footerDocument()
*/
void QtPrinterListView::setFooterText(const QString &footer)
{
    Q_D(QtPrinterListView);
    d->getOrCreateFooter()->setPlainText(footer);
}

/*!
  Return the text set as header.
  \sa headerDocument()
*/
QString QtPrinterListView::headerText() const
{
    Q_D(const QtPrinterListView);
    return d->getOrCreateHeader()->toPlainText();
}

/*!
  Return the text set as footer.
  \sa headerDocument()
*/
QString QtPrinterListView::footerText() const
{
    Q_D(const QtPrinterListView);
    if (d->footer == 0)
        return QString();
    return d->footer->toPlainText();
}

QtGraphicsListViewItemCreatorBase *QtPrinterListView::viewItemCreator() const
{
    Q_D(const QtPrinterListView);
    return d->creator;
}

/*!
  note that ownership is taken.
*/
void QtPrinterListView::setViewItemCreator(QtGraphicsListViewItemCreatorBase *creator)
{
    Q_D(QtPrinterListView);
    delete d->creator;
    d->creator = creator;
}

extern int qt_defaultDpiY();
extern int qt_defaultDpiX();

/*!
  Start the actual printing using the printer() options.
  Returns true if printing was successful, or false otherwise.
*/
bool QtPrinterListView::print()
{
    Q_D(QtPrinterListView);
    if (! d->printer->isValid()) {
        qWarning() << "QtPrinterListView::print: printer not valid, please setup the printer before calling print";
        return false;
    }

    // next use a view just for the filling of the options...
    QtGraphicsListView view;
    view.setModel(d->model);

    QPainter painter;
    if (!painter.begin(d->printer)) {
        qWarning() << "QtPrinterListView::print: printer fails to begin(), sorry can't print";
        return false;
    }

    // re-layout the header footer to the current page size.
    if (d->header) {
        d->header->documentLayout()->setPaintDevice(d->printer);
        d->header->setPageSize(d->printer->pageRect().size());
    }
    if (d->footer) {
        d->footer->documentLayout()->setPaintDevice(d->printer);
        d->footer->setPageSize(d->printer->pageRect().size());
    }

    QtGraphicsListViewItem *listViewItem;
    if (d->creator)
        listViewItem = d->creator->create(0, &view);
    else
        listViewItem = view.itemCreator()->create(0, &view);
    listViewItem->setCacheMode(QGraphicsItem::NoCache);

    QStyleOptionViewItemV4 widgetOptions;
    QStyleOptionGraphicsItem graphicsViewOptions;
    view.initStyleOption(&widgetOptions);

    const qreal headerSize = d->printHeader(painter);
    const qreal footerSize = d->printFooter(painter);
    const bool vertical = d->orientation == Qt::Vertical;
    const qreal scaleX = d->printer->logicalDpiX() / (qreal) qt_defaultDpiX();
    const qreal scaleY = d->printer->logicalDpiY() / (qreal) qt_defaultDpiY();
    QSizeF constraint = d->printer->pageRect().size();
    // cache the space we have for actual list items.
    const qreal contentSize = vertical ? constraint.height() - headerSize - footerSize : constraint.width();

    view.resize(constraint - QSizeF(0, headerSize + footerSize));

    // a constraint is -1 in the direction we allow items to grow in.
    if (vertical) {
        constraint.setHeight(-1);
    } else {
        constraint.setWidth(-1);
        // in this direction headers/footers eat our precious space
        constraint.setHeight(constraint.height() - headerSize -  - footerSize);
    }

    qreal progression = vertical ? headerSize : 0; // init
    const int max = d->model->count();
    for (int index = 0; index < max; ++index) {
        view.initStyleOption(&widgetOptions, index);

        //QSizeF itemSize = listViewItem->sizeHint(index, &widgetOptions, Qt::PreferredSize, constraint);
        listViewItem->setIndex(index);
        QRectF geometry = view.itemGeometry(index);
        if (vertical)
            geometry.setWidth(constraint.width() / scaleX);
        else
            geometry.setHeight(constraint.height() / scaleY - headerSize - footerSize);
        listViewItem->setGeometry(geometry);
        geometry = listViewItem->geometry();// the listViewItem can be stubborn
        graphicsViewOptions.rect = geometry.toRect();
        if ((vertical && progression - headerSize + geometry.height() > contentSize)
                || (!vertical && progression + geometry.width()> contentSize)) {
            if (! d->printer->newPage()) {
                qWarning() << "QtPrinterListView::print: Failed to start a new page, aborting printing";
                return false;
            }
            progression = vertical ? headerSize : 0; // reset
            d->printHeader(painter);
            d->printFooter(painter);
        }

        painter.save();
        painter.translate(vertical ? 0 : progression,
                vertical ? progression : headerSize);
        painter.scale(scaleX, scaleY);
        listViewItem->paint(&painter, &graphicsViewOptions);
        painter.restore();
        progression += vertical ? geometry.height() * scaleY: geometry.width() * scaleX;
    }
    return true;
}

/*!
  Prints the \a model to the \a printer
*/
//static
void QtPrinterListView::printModel(QPrinter *printer, QtListModelInterface *model, const QString &header, const QString &footer)
{
    QtPrinterListView printerView(printer, model);
    if (!header.isEmpty())
        printerView.setHeaderText(header);
    if (!footer.isEmpty())
        printerView.setFooterText(footer);

    printerView.print();
}

QT_END_NAMESPACE

#endif