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         resizeContents(
00097             QMAX(re->size().width(), widget->width()+2*KexiSimplePrintPreviewScrollView_MARGIN),
00098             QMAX(re->size().height(), widget->height()+2*KexiSimplePrintPreviewScrollView_MARGIN));
00099         int vscrbarWidth = verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0;
00100         moveChild(widget, (contentsWidth() - vscrbarWidth - widget->width())/2, 
00101             (contentsHeight() - widget->height())/2);
00102     }
00103     setUpdatesEnabled(true);
00104 }
00105 
00106 void KexiSimplePrintPreviewScrollView::setFullWidth()
00107 {
00108     viewport()->setUpdatesEnabled(false);
00109     double widthMM = KoPageFormat::width( 
00110         m_window->settings().pageLayout.format, 
00111         m_window->settings().pageLayout.orientation);
00112     double heightMM = KoPageFormat::height( 
00113         m_window->settings().pageLayout.format, m_window->settings().pageLayout.orientation);
00114 //  int constantWidth = m_window->width()- KexiSimplePrintPreviewScrollView_MARGIN*6;
00115     double constantWidth = width()- KexiSimplePrintPreviewScrollView_MARGIN*6;
00116     double heightForWidth = constantWidth * heightMM / widthMM;
00117 //  heightForWidth = QMIN(kapp->desktop()->height()*4/5, heightForWidth);
00118     kdDebug() << "1: " << heightForWidth << endl;
00119 #if 0 //todo we can use this if we want to fix the height to width of the page
00120     heightForWidth = QMIN(height(), heightForWidth);
00121     kdDebug() << "2: " << heightForWidth << endl;
00122 #endif
00123     constantWidth = heightForWidth * widthMM / heightMM;
00124     widget->resize((int)constantWidth, (int)heightForWidth); //keep aspect
00125     resizeContents(int(widget->width() + 2*KexiSimplePrintPreviewScrollView_MARGIN), 
00126         int(widget->height() + 2*KexiSimplePrintPreviewScrollView_MARGIN));
00127     moveChild(widget, (contentsWidth()-widget->width())/2, 
00128         (contentsHeight()-widget->height())/2);
00129     viewport()->setUpdatesEnabled(true);
00130     resize(size()+QSize(1,1)); //to update pos.
00131     widget->enablePainting = true;
00132     widget->repaint();
00133 }
00134 
00135 void KexiSimplePrintPreviewScrollView::setContentsPos(int x, int y)
00136 {
00137 //  kdDebug() << "############" << x << " " << y << " " << contentsX()<< " " <<contentsY() << endl;
00138     if (x<0 || y<0) //to avoid endless loop on Linux
00139         return;
00140     QScrollView::setContentsPos(x,y);
00141 }
00142 
00143 //------------------
00144 
00145 KexiSimplePrintPreviewWindow::KexiSimplePrintPreviewWindow(
00146     KexiSimplePrintingEngine &engine, const QString& previewName, 
00147     QWidget *parent, WFlags f)
00148  : QWidget(parent, "KexiSimplePrintPreviewWindow", f)
00149  , m_engine(engine)
00150  , m_settings(*m_engine.settings())
00151  , m_pageNumber(-1)
00152 {
00153 //  m_pagesCount = INT_MAX;
00154 
00155     setCaption(i18n("%1 - Print Preview - %2").arg(previewName).arg(KEXI_APP_NAME));
00156     setIcon(DesktopIcon("filequickprint"));
00157     QVBoxLayout *lyr = new QVBoxLayout(this, 6);
00158 
00159     int id;
00160     m_toolbar = new KToolBar(0, this);
00161     m_toolbar->setLineWidth(0);
00162     m_toolbar->setFrameStyle(QFrame::NoFrame);
00163     m_toolbar->setIconText(KToolBar::IconTextRight);
00164     lyr->addWidget(m_toolbar);
00165 
00166     id = m_toolbar->insertWidget( -1, 0, new KPushButton(KStdGuiItem::print(), m_toolbar) );
00167     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotPrintClicked()));
00168     static_cast<KPushButton*>(m_toolbar->getWidget(id))->setAccel(Qt::CTRL|Qt::Key_P);
00169     m_toolbar->insertSeparator();
00170 
00171     id = m_toolbar->insertWidget(-1, 0, new KPushButton(i18n("Page Set&up..."), m_toolbar));
00172     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotPageSetup()));
00173     m_toolbar->insertSeparator();
00174 
00175 
00176 #ifndef KEXI_NO_UNFINISHED 
00178     id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag+"), i18n("Zoom In"), m_toolbar));
00179     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomInClicked()));
00180     m_toolbar->insertSeparator();
00181 
00182     id = m_toolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("viewmag-"), i18n("Zoom Out"), m_toolbar));
00183     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(slotZoomOutClicked()));
00184     m_toolbar->insertSeparator();
00185 #endif
00186 
00187     id = m_toolbar->insertWidget(-1, 0, new KPushButton(KStdGuiItem::close(), m_toolbar));
00188     m_toolbar->addConnection(id, SIGNAL(clicked()), this, SLOT(close()));
00189     m_toolbar->alignItemRight(id);
00190 
00191     m_scrollView = new KexiSimplePrintPreviewScrollView(this);
00192     m_scrollView->setUpdatesEnabled(false);
00193     m_view = m_scrollView->widget;
00194     m_scrollView->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
00195     lyr->addWidget(m_scrollView);
00196 
00197     m_navToolbar = new KToolBar(0, this);
00198 //  m_navToolbar->setFullWidth(true);
00199     m_navToolbar->setLineWidth(0);
00200     m_navToolbar->setFrameStyle(QFrame::NoFrame);
00201     m_navToolbar->setIconText(KToolBar::IconTextRight);
00202     lyr->addWidget(m_navToolbar);
00203 
00204     m_idFirst = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("start"), i18n("First Page"), m_navToolbar));
00205     m_navToolbar->addConnection(m_idFirst, SIGNAL(clicked()), this, SLOT(slotFirstClicked()));
00206     m_navToolbar->insertSeparator();
00207 
00208     m_idPrevious = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("previous"), i18n("Previous Page"), m_navToolbar));
00209     m_navToolbar->addConnection(m_idPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousClicked()));
00210     m_navToolbar->insertSeparator();
00211 
00212     m_idPageNumberLabel = m_navToolbar->insertWidget( -1, 0, new QLabel(m_navToolbar));
00213     m_navToolbar->insertSeparator();
00214 
00215     m_idNext = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("next"), i18n("Next Page"), m_navToolbar));
00216     m_navToolbar->addConnection(m_idNext, SIGNAL(clicked()), this, SLOT(slotNextClicked()));
00217     m_navToolbar->insertSeparator();
00218 
00219     m_idLast = m_navToolbar->insertWidget( -1, 0, new KPushButton(BarIconSet("finish"), i18n("Last Page"), m_navToolbar));
00220     m_navToolbar->addConnection(m_idLast, SIGNAL(clicked()), this, SLOT(slotLastClicked()));
00221     m_navToolbar->insertSeparator();
00222 
00223     resize(width(), kapp->desktop()->height()*4/5);
00224 
00226 
00227     QTimer::singleShot(50, this, SLOT(initLater()));
00228 }
00229 
00230 void KexiSimplePrintPreviewWindow::initLater()
00231 {
00232     setFullWidth();
00233     updatePagesCount();
00234     goToPage(0);
00235 }
00236 
00237 KexiSimplePrintPreviewWindow::~KexiSimplePrintPreviewWindow()
00238 {
00239 }
00240 
00241 /*void KexiSimplePrintPreviewWindow::setPagesCount(int pagesCount)
00242 {
00243     m_pagesCount = pagesCount;
00244     goToPage(0);
00245 }*/
00246 
00247 void KexiSimplePrintPreviewWindow::slotPrintClicked()
00248 {
00249     hide();
00250     emit printRequested();
00251     show();
00252     raise();
00253 }
00254 
00255 void KexiSimplePrintPreviewWindow::slotPageSetup()
00256 {
00257     lower();
00258     emit pageSetupRequested();
00259 }
00260 
00261 void KexiSimplePrintPreviewWindow::slotZoomInClicked()
00262 {
00264 }
00265 
00266 void KexiSimplePrintPreviewWindow::slotZoomOutClicked()
00267 {
00269 }
00270 
00271 void KexiSimplePrintPreviewWindow::slotFirstClicked()
00272 {
00273     goToPage(0);
00274 }
00275 
00276 void KexiSimplePrintPreviewWindow::slotPreviousClicked()
00277 {
00278     goToPage(m_pageNumber-1);
00279 }
00280 
00281 void KexiSimplePrintPreviewWindow::slotNextClicked()
00282 {
00283     goToPage(m_pageNumber+1);
00284 }
00285 
00286 void KexiSimplePrintPreviewWindow::slotLastClicked()
00287 {
00288     goToPage(m_engine.pagesCount()-1);
00289 }
00290 
00291 void KexiSimplePrintPreviewWindow::goToPage(int pageNumber)
00292 {
00293     if (pageNumber==m_pageNumber || pageNumber < 0 || pageNumber > ((int)m_engine.pagesCount()-1))
00294         return;
00295     m_pageNumber = pageNumber;
00296 
00297     m_view->repaint(); //this will automatically paint a new page
00298 //  if (m_engine.eof())
00299 //      m_pagesCount = pageNumber+1;
00300 
00301     m_navToolbar->setItemEnabled(m_idNext, pageNumber < ((int)m_engine.pagesCount()-1));
00302     m_navToolbar->setItemEnabled(m_idLast, pageNumber < ((int)m_engine.pagesCount()-1));
00303     m_navToolbar->setItemEnabled(m_idPrevious, pageNumber > 0);
00304     m_navToolbar->setItemEnabled(m_idFirst, pageNumber > 0);
00305     static_cast<QLabel*>(m_navToolbar->getWidget(m_idPageNumberLabel))->setText(
00306         i18n("Page (number) of (total)", "Page %1 of %2").arg(m_pageNumber+1).arg(m_engine.pagesCount()));
00307 }
00308 
00309 void KexiSimplePrintPreviewWindow::setFullWidth()
00310 {
00311     m_scrollView->setFullWidth();
00312 }
00313 
00314 void KexiSimplePrintPreviewWindow::updatePagesCount()
00315 {
00316     QPixmap pm(m_view->size()); //dbl buffered
00317     QPainter p(m_view);
00318     //p.begin(&pm, this);
00320 //  p.fillRect(pe->rect(), QBrush(white));
00321     m_engine.calculatePagesCount(p);
00322     p.end();
00323 }
00324 
00325 bool KexiSimplePrintPreviewWindow::event( QEvent * e )
00326 {
00327     QEvent::Type t = e->type();
00328     if (t==QEvent::KeyPress) {
00329         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00330         const int k = ke->key();
00331         bool ok = true;
00332         if (k==Qt::Key_Equal || k==Qt::Key_Plus)
00333             slotZoomInClicked();
00334         else if (k==Qt::Key_Minus)
00335             slotZoomOutClicked();
00336         else if (k==Qt::Key_Home)
00337             slotFirstClicked();
00338         else if (k==Qt::Key_End)
00339             slotLastClicked();
00340         else
00341             ok = false;
00342 
00343         if (ok) {
00344             ke->accept();
00345             return true;
00346         }
00347     }
00348     else if (t==QEvent::AccelOverride) {
00349         QKeyEvent *ke = static_cast<QKeyEvent*>(e);
00350         const int k = ke->key();
00351         bool ok = true;
00352         if (k==Qt::Key_PageUp)
00353             slotPreviousClicked();
00354         else if (k==Qt::Key_PageDown)
00355             slotNextClicked();
00356         else
00357             ok = false;
00358 
00359         if (ok) {
00360             ke->accept();
00361             return true;
00362         }
00363     }
00364     return QWidget::event(e);
00365 }
00366 
00367 
00368 #include "kexisimpleprintpreviewwindow.moc"
00369 #include "kexisimpleprintpreviewwindow_p.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys