kexi

kexisimpleprintpreviewwindow.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexisimpleprintpreviewwindow.h"
00021 #include "kexisimpleprintingengine.h"
00022 #include "kexisimpleprintpreviewwindow_p.h"
00023 #include <kexi_version.h>
00024 
00025 #include <qlayout.h>
00026 #include <qaccel.h>
00027 #include <qtimer.h>
00028 #include <qlabel.h>
00029 
00030 #include <kdialogbase.h>
00031 #include <ktoolbarbutton.h>
00032 #include <klocale.h>
00033 #include <kiconloader.h>
00034 #include <kdebug.h>
00035 #include <kpushbutton.h>
00036 #include <kapplication.h>
00037 
00038 KexiSimplePrintPreviewView::KexiSimplePrintPreviewView(
00039     QWidget *parent, KexiSimplePrintPreviewWindow *window)
00040  : QWidget(parent, "KexiSimplePrintPreviewView", WStaticContents)//|WNoAutoErase)
00041  , m_window(window)
00042 {
00043     enablePainting = false;
00044 //          resize(300,400);
00045 //          resizeContents(200, 400);
00046 }
00047 
00048 void KexiSimplePrintPreviewView::paintEvent( QPaintEvent *pe )
00049 {
00050     Q_UNUSED(pe);
00051     if (!enablePainting)
00052         return;
00053     QPixmap pm(size()); //dbl buffered
00054     QPainter p;
00055     p.begin(&pm, this);
00057     p.fillRect(QRect(QPoint(0,0),pm.size()), QBrush(white));//pe->rect(), QBrush(white));
00058     if (m_window->currentPage()>=0)
00059         m_window->m_engine.paintPage(m_window->currentPage(), p);
00060 //      emit m_window->paintingPageRequested(m_window->currentPage(), p);
00061     p.end();
00062     bitBlt(this, 0, 0, &pm);
00063 }
00064 
00065 //--------------------------
00066 
00067 #define KexiSimplePrintPreviewScrollView_MARGIN KDialogBase::marginHint()
00068 
00069 KexiSimplePrintPreviewScrollView::KexiSimplePrintPreviewScrollView(
00070     KexiSimplePrintPreviewWindow *window)
00071  : QScrollView(window, "scrollview", WStaticContents|WNoAutoErase)
00072  , m_window(window)
00073 {
00074 //          this->settings = settings;
00075     widget = new KexiSimplePrintPreviewView(viewport(), m_window);
00076 
00077 /*          int widthMM = KoPageFormat::width( 
00078         settings.pageLayout.format, settings.pageLayout.orientation);
00079     int heightMM = KoPageFormat::height( 
00080         settings.pageLayout.format, settings.pageLayout.orientation);
00081 //          int constantHeight = 400;
00082 //          widget->resize(constantHeight * widthMM / heightMM, constantHeight ); //keep aspect
00083 */
00084     addChild(widget);
00085 }
00086 
00087 void KexiSimplePrintPreviewScrollView::resizeEvent( QResizeEvent *re )
00088 {
00089     QScrollView::resizeEvent(re);
00090 //  kdDebug() << re->size().width() << " " << re->size().height() << endl;
00091 //  kdDebug() << contentsWidth() << " " << contentsHeight() << endl;
00092 //  kdDebug() << widget->width() << " " << widget->height() << endl;
00093     setUpdatesEnabled(false);
00094     if (re->size().width() > (widget->width()+2*KexiSimplePrintPreviewScrollView_MARGIN)
00095         || re->size().height() > (widget->height()+2*KexiSimplePrintPreviewScrollView_MARGIN))
00096     {
00097         resizeContents(
00098             QMAX(re->size().width(), widget->width()+2*KexiSimplePrintPreviewScrollView_MARGIN),
00099             QMAX(re->size().height(), widget->height()+2*KexiSimplePrintPreviewScrollView_MARGIN));
00100         int vscrbarWidth = verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0;
00101         int newContentsWidth
00102             = QMAX(re->size().width(), widget->width()+2*KexiSimplePrintPreviewScrollView_MARGIN);
00103         int newContentsHeight
00104             = QMAX(re->size().height(), widget->height()+2*KexiSimplePrintPreviewScrollView_MARGIN);
00105         moveChild(widget, (newContentsWidth - vscrbarWidth - widget->width())/2, 
00106             (newContentsHeight - widget->height())/2);
00107         resizeContents( newContentsWidth, newContentsHeight );
00108     }
00109     setUpdatesEnabled(true);
00110 }
00111 
00112 void KexiSimplePrintPreviewScrollView::setFullWidth()
00113 {
00114     viewport()->setUpdatesEnabled(false);
00115     double widthMM = KoPageFormat::width( 
00116         m_window->settings().pageLayout.format, 
00117         m_window->settings().pageLayout.orientation);
00118     double heightMM = KoPageFormat::height( 
00119         m_window->settings().pageLayout.format, m_window->settings().pageLayout.orientation);
00120 //  int constantWidth = m_window->width()- KexiSimplePrintPreviewScrollView_MARGIN*6;
00121     double constantWidth = width()- KexiSimplePrintPreviewScrollView_MARGIN*6;
00122     double heightForWidth = constantWidth * heightMM / widthMM;
00123 //  heightForWidth = QMIN(kapp->desktop()->height()*4/5, heightForWidth);
00124     kdDebug() << "1: " << heightForWidth << endl;
00125 #if 0 //todo we can use this if we want to fix the height to width of the page
00126     heightForWidth = QMIN(height(), heightForWidth);
00127     kdDebug() << "2: " << heightForWidth << endl;
00128 #endif
00129     constantWidth = heightForWidth * widthMM / heightMM;
00130     widget->resize((int)constantWidth, (int)heightForWidth); //keep aspect
00131     resizeContents(int(widget->width() + 2*KexiSimplePrintPreviewScrollView_MARGIN), 
00132         int(widget->height() + 2*KexiSimplePrintPreviewScrollView_MARGIN));
00133     moveChild(widget, (contentsWidth()-widget->width())/2, 
00134         (contentsHeight()-widget->height())/2);
00135     viewport()->setUpdatesEnabled(true);
00136     resize(size()+QSize(1,1)); //to update pos.
00137     widget->enablePainting = true;
00138     widget->repaint();
00139 }
00140 
00141 void KexiSimplePrintPreviewScrollView::setContentsPos(int x, int y)
00142 {
00143 //  kdDebug() << "############" << x << " " << y << " " << contentsX()<< " " <<contentsY() << endl;
00144     if (x<0 || y<0) //to avoid endless loop on Linux
00145         return;
00146     QScrollView::setContentsPos(x,y);
00147 }
00148 
00149 //------------------
00150 
00151 KexiSimplePrintPreviewWindow::KexiSimplePrintPreviewWindow(
00152     KexiSimplePrintingEngine &engine, const QString& previewName, 
00153     QWidget *parent, WFlags f)
00154  : QWidget(parent, "KexiSimplePrintPreviewWindow", f)
00155  , m_engine(engine)
00156  , m_settings(*m_engine.settings())
00157  , m_pageNumber(-1)
00158  , m_pagesCount(-1)
00159 {
00160     setCaption(i18n("%1 - Print Preview - %2").arg(previewName).arg(KEXI_APP_NAME));
00161     setIcon(DesktopIcon("filequickprint"));
00162     QVBoxLayout *lyr = new QVBoxLayout(this, 6);
00163 
00164     int id;
00165     m_toolbar = new KToolBar(0, this);
00166     m_toolbar->setLineWidth(0);
00167     m_toolbar->setFrameStyle(QFrame::NoFrame);
00168     m_toolbar->setIconText(KToolBar::IconTextRight);
00169     lyr->addWidget(m_toolbar);
00170 
00171     id = m_toolbar->insertWidget( -1, 0, new KPushButton(KStdGuiItem::print(), m_toolbar) );
00172     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotPrintClicked()));
00173     static_cast<KPushButton*>(m_toolbar->getWidget(id))->setAccel(Qt::CTRL|Qt::Key_P);
00174     m_toolbar->insertSeparator();
00175 
00176     id = m_toolbar->insertWidget(-1, 0, new KPushButton(i18n("Page Set&up..."), m_toolbar));
00177     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotPageSetup()));
00178     m_toolbar->insertSeparator();
00179 
00180 
00181 #ifndef KEXI_NO_UNFINISHED 
00183     id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag+"), i18n("Zoom In"), m_toolbar));
00184     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomInClicked()));
00185     m_toolbar->insertSeparator();
00186 
00187     id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag-"), i18n("Zoom Out"), m_toolbar));
00188     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomOutClicked()));
00189     m_toolbar->insertSeparator();
00190 #endif
00191 
00192     id = m_toolbar->insertWidget(-1, 0, new KPushButton(KStdGuiItem::close(), m_toolbar));
00193     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(close()));
00194     m_toolbar->alignItemRight(id);
00195 
00196     m_scrollView = new KexiSimplePrintPreviewScrollView(this);
00197     m_scrollView->setUpdatesEnabled(false);
00198     m_view = m_scrollView->widget;
00199     m_scrollView->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
00200     lyr->addWidget(m_scrollView);
00201 
00202     QWidget* navToolbarWidget = new QWidget(this); //widget used to center the navigator toolbar
00203     navToolbarWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00204     QHBoxLayout *navToolbarLyr = new QHBoxLayout(navToolbarWidget);
00205     lyr->addWidget(navToolbarWidget);
00206 
00207     m_navToolbar = new KToolBar(0, navToolbarWidget);
00208     navToolbarLyr->addStretch(1);
00209     navToolbarLyr->addWidget(m_navToolbar);
00210     navToolbarLyr->addStretch(1);
00211 //  m_navToolbar->setFullWidth(true);
00212     m_navToolbar->setLineWidth(0);
00213     m_navToolbar->setFrameStyle(QFrame::NoFrame);
00214     m_navToolbar->setIconText(KToolBar::IconTextRight);
00215 
00216     m_idFirst = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("start"), i18n("First Page"), m_navToolbar));
00217     m_navToolbar->addConnection(m_idFirst, SIGNAL(clicked()), this, SLOT(slotFirstClicked()));
00218     m_navToolbar->insertSeparator();
00219 
00220     m_idPrevious = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("previous"), i18n("Previous Page"), m_navToolbar));
00221     m_navToolbar->addConnection(m_idPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousClicked()));
00222     m_navToolbar->insertSeparator();
00223 
00224     m_idPageNumberLabel = m_navToolbar->insertWidget( -1, 0, new QLabel(m_navToolbar));
00225     m_navToolbar->insertSeparator();
00226 
00227     m_idNext = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("next"), i18n("Next Page"), m_navToolbar));
00228     m_navToolbar->addConnection(m_idNext, SIGNAL(clicked()), this, SLOT(slotNextClicked()));
00229     m_navToolbar->insertSeparator();
00230 
00231     m_idLast = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("finish"), i18n("Last Page"), m_navToolbar));
00232     m_navToolbar->addConnection(m_idLast, SIGNAL(clicked()), this, SLOT(slotLastClicked()));
00233     m_navToolbar->insertSeparator();
00234 
00235     resize(width(), kapp->desktop()->height()*4/5);
00236 
00238 
00239     QTimer::singleShot(50, this, SLOT(initLater()));
00240 }
00241 
00242 void KexiSimplePrintPreviewWindow::initLater()
00243 {
00244     setFullWidth();
00245     updatePagesCount();
00246     goToPage(0);
00247 }
00248 
00249 KexiSimplePrintPreviewWindow::~KexiSimplePrintPreviewWindow()
00250 {
00251 }
00252 
00253 /*void KexiSimplePrintPreviewWindow::setPagesCount(int pagesCount)
00254 {
00255     m_pagesCount = pagesCount;
00256     goToPage(0);
00257 }*/
00258 
00259 void KexiSimplePrintPreviewWindow::slotPrintClicked()
00260 {
00261     hide();
00262     emit printRequested();
00263     show();
00264     raise();
00265 }
00266 
00267 void KexiSimplePrintPreviewWindow::slotPageSetup()
00268 {
00269     lower();
00270     emit pageSetupRequested();
00271 }
00272 
00273 void KexiSimplePrintPreviewWindow::slotZoomInClicked()
00274 {
00276 }
00277 
00278 void KexiSimplePrintPreviewWindow::slotZoomOutClicked()
00279 {
00281 }
00282 
00283 void KexiSimplePrintPreviewWindow::slotFirstClicked()
00284 {
00285     goToPage(0);
00286 }
00287 
00288 void KexiSimplePrintPreviewWindow::slotPreviousClicked()
00289 {
00290     goToPage(m_pageNumber-1);
00291 }
00292 
00293 void KexiSimplePrintPreviewWindow::slotNextClicked()
00294 {
00295     goToPage(m_pageNumber+1);
00296 }
00297 
00298 void KexiSimplePrintPreviewWindow::slotLastClicked()
00299 {
00300     goToPage(m_engine.pagesCount()-1);
00301 }
00302 
00303 void KexiSimplePrintPreviewWindow::goToPage(int pageNumber)
00304 {
00305     if ((pageNumber==m_pageNumber && m_pagesCount == (int)m_engine.pagesCount()) 
00306         || pageNumber < 0 || pageNumber > ((int)m_engine.pagesCount()-1))
00307         return;
00308     m_pageNumber = pageNumber;
00309     m_pagesCount = m_engine.pagesCount();
00310 
00311     m_view->repaint(); //this will automatically paint a new page
00312 
00313     m_navToolbar->setItemEnabled(m_idNext, pageNumber < ((int)m_engine.pagesCount()-1));
00314     m_navToolbar->setItemEnabled(m_idLast, pageNumber < ((int)m_engine.pagesCount()-1));
00315     m_navToolbar->setItemEnabled(m_idPrevious, pageNumber > 0);
00316     m_navToolbar->setItemEnabled(m_idFirst, pageNumber > 0);
00317     static_cast<QLabel*>(m_navToolbar->getWidget(m_idPageNumberLabel))->setText(
00318         i18n("Page (number) of (total)", "Page %1 of %2").arg(m_pageNumber+1).arg(m_engine.pagesCount()));
00319 }
00320 
00321 void KexiSimplePrintPreviewWindow::setFullWidth()
00322 {
00323     m_scrollView->setFullWidth();
00324 }
00325 
00326 void KexiSimplePrintPreviewWindow::updatePagesCount()
00327 {
00328     QPixmap pm(m_view->size()); //dbl buffered
00329     QPainter p(m_view);
00330     //p.begin(&pm, this);
00332 //  p.fillRect(pe->rect(), QBrush(white));
00333     m_engine.calculatePagesCount(p);
00334     p.end();
00335 }
00336 
00337 bool KexiSimplePrintPreviewWindow::event( QEvent * e )
00338 {
00339     QEvent::Type t = e->type();
00340     if (t==QEvent::KeyPress) {
00341         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00342         const int k = ke->key();
00343         bool ok = true;
00344         if (k==Qt::Key_Equal || k==Qt::Key_Plus)
00345             slotZoomInClicked();
00346         else if (k==Qt::Key_Minus)
00347             slotZoomOutClicked();
00348         else if (k==Qt::Key_Home)
00349             slotFirstClicked();
00350         else if (k==Qt::Key_End)
00351             slotLastClicked();
00352         else
00353             ok = false;
00354 
00355         if (ok) {
00356             ke->accept();
00357             return true;
00358         }
00359     }
00360     else if (t==QEvent::AccelOverride) {
00361         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00362         const int k = ke->key();
00363         bool ok = true;
00364         if (k==Qt::Key_PageUp)
00365             slotPreviousClicked();
00366         else if (k==Qt::Key_PageDown)
00367             slotNextClicked();
00368         else
00369             ok = false;
00370 
00371         if (ok) {
00372             ke->accept();
00373             return true;
00374         }
00375     }
00376     return QWidget::event(e);
00377 }
00378 
00379 
00380 #include "kexisimpleprintpreviewwindow.moc"
00381 #include "kexisimpleprintpreviewwindow_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys