diff --git a/src/doc/user/usermanual.sgml b/src/doc/user/usermanual.sgml index d93375ea..d0b84d42 100644 --- a/src/doc/user/usermanual.sgml +++ b/src/doc/user/usermanual.sgml @@ -854,6 +854,11 @@ fvwm associated to the document (ie: author, abtract, etc.). This is especially useful in cases where the term match did not occur in the main text but in one of the fields. + + You can print the current preview window contents by typing + ^P (Ctrl + P) in + the window text. + @@ -1462,6 +1467,11 @@ fvwm window and all its tabs. + Printing previews + Entering ^P in a preview window will print + the currently displayed text. + + Quitting Entering ^Q almost anywhere will close the application. diff --git a/src/qtgui/preview_w.cpp b/src/qtgui/preview_w.cpp index 7d1145e2..0fcfae63 100644 --- a/src/qtgui/preview_w.cpp +++ b/src/qtgui/preview_w.cpp @@ -33,18 +33,28 @@ using std::pair; #include #include #include +#include +#include + #if (QT_VERSION < 0x040000) #include #include #include #define THRFINISHED finished +#include +#include #else #include #include #include #include +#include +#define QPaintDeviceMetrics Q3PaintDeviceMetrics +#include +#define QSimpleRichText Q3SimpleRichText #define THRFINISHED isFinished #endif + #include #include #include @@ -204,6 +214,8 @@ void Preview::init() connect(pvTab, SIGNAL(currentChanged(QWidget *)), this, SLOT(currentChanged(QWidget *))); connect(bt, SIGNAL(clicked()), this, SLOT(closeCurrentTab())); + connect(this, SIGNAL(printCurrentPreviewRequest()), + this, SLOT(printCurrent())); m_dynSearchActive = false; m_canBeep = true; @@ -266,6 +278,11 @@ bool Preview::eventFilter(QObject *target, QEvent *event) // LOGDEB(("Preview::eventFilter: got ^W\n")); closeCurrentTab(); return true; + } else if (keyEvent->key() == Qt::Key_P && + (keyEvent->state() & Qt::ControlButton)) { + // LOGDEB(("Preview::eventFilter: got ^P\n")); + emit(printCurrentPreviewRequest()); + return true; } else if (m_dynSearchActive) { if (keyEvent->key() == Qt::Key_F3) { doSearch(searchTextLine->text(), true, false); @@ -449,6 +466,64 @@ void Preview::currentChanged(QWidget * tw) emit(previewExposed(this, m_searchId, d->docnum)); } +void Preview::printCurrent() +{ + PreviewTextEdit *edit = Preview::getCurrentEditor(); + if (edit == 0) + return; + TabData *d = tabDataForCurrent(); + if (d == 0) + return; + +#ifndef QT_NO_PRINTER + QPrinter printer; + QPrintDialog *dialog = new QPrintDialog(&printer, this); +#if (QT_VERSION >= 0x040000) + dialog->setWindowTitle(tr("Print Current Preview")); +#endif + if (dialog->exec() != QDialog::Accepted) + return; + + // A qt4 version of this would just be : + // edit->document()->print(&printer); But as we are using a + // q3textedit, we have to do the q3 printing dance, even under + // qt4. The following code is taken from + // qt3/examples/textdrawing/qtextedit.cpp + printer.setFullPage(TRUE); + QPaintDeviceMetrics screen( edit ); + printer.setResolution( screen.logicalDpiY() ); + QPainter p( &printer ); + QPaintDeviceMetrics metrics( p.device() ); + int dpix = metrics.logicalDpiX(); + int dpiy = metrics.logicalDpiY(); + const int margin = 72; // pt + QRect body( margin * dpix / 72, margin * dpiy / 72, + metrics.width() - margin * dpix / 72 * 2, + metrics.height() - margin * dpiy / 72 * 2 ); + QFont font( "times", 10 ); + // Dont want to use edit->text() here, this is the plain text. We + // want the rich text. + QSimpleRichText richText(d->richtxt, font, edit->context(), + edit->styleSheet(), + edit->mimeSourceFactory(), body.height() ); + richText.setWidth( &p, body.width() ); + QRect view( body ); + int page = 1; + do { + richText.draw( &p, body.left(), body.top(), view, colorGroup() ); + view.moveBy( 0, body.height() ); + p.translate( 0 , -body.height() ); + p.setFont( font ); + p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ), + view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) ); + if ( view.top() >= richText.height() ) + break; + printer.newPage(); + page++; + } while (TRUE); +#endif +} + #if (QT_VERSION >= 0x040000) // I have absolutely no idea why this nonsense is needed to get // q3textedit to copy to x11 primary selection when text is @@ -912,6 +987,7 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc, item->setColor(prefs.qtermcolor); item->setFontWeight(QFont::Bold); } + TabData *d = tabDataForCurrent(); prog = 2 * nsteps / 3; progress.setLabelText(tr("Loading preview text into editor")); @@ -923,6 +999,10 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc, qApp->processEvents(); editor->append(*it); + // We need to save the rich text for printing, the editor does + // not do it for us + if (d) + d->richtxt.append(*it); // Stay at top if (instep < 5) { @@ -938,9 +1018,8 @@ bool Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc, } progress.close(); - TabData *d = tabDataForCurrent(); if (d) { - fdoc.text.clear(); + fdoc.text.clear(); d->fdoc = fdoc; } m_haveAnchors = m_plaintorich.lastanchor != 0; diff --git a/src/qtgui/preview_w.h b/src/qtgui/preview_w.h index 41219eeb..d57b22c7 100644 --- a/src/qtgui/preview_w.h +++ b/src/qtgui/preview_w.h @@ -74,6 +74,7 @@ class TabData { // doc out of internfile (previous fields come from the index) with // main text erased (for space). Rcl::Doc fdoc; + QString richtxt; TabData(QWidget *wi) : w(wi), docnum(-1) {} @@ -141,6 +142,7 @@ public slots: virtual void closeCurrentTab(); virtual void textDoubleClicked(int, int); virtual void selecChanged(); + virtual void printCurrent(); signals: void previewClosed(Preview *); @@ -148,6 +150,7 @@ signals: void showNext(Preview *w, int sid, int docnum); void showPrev(Preview *w, int sid, int docnum); void previewExposed(Preview *w, int sid, int docnum); + void printCurrentPreviewRequest(); private: // Identifier of search in main window. This is used to check that