File indexing completed on 2024-05-19 15:27:51

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 /* This file was part of the KDE project
0020    Copyright (C) 2005 Jarosław Staniek <staniek@kde.org>
0021 
0022    This program is free software; you can redistribute it and/or
0023    modify it under the terms of the GNU Library General Public
0024    License as published by the Free Software Foundation; either
0025    version 2 of the License, or (at your option) any later version.
0026  */
0027 
0028 // Description: Page Layout Dialog (sources)
0029 
0030 /******************************************************************/
0031 
0032 #include "kgraphviewerlib_debug.h"
0033 #include <KgvGlobal.h>
0034 #include <KgvPageLayoutColumns.h>
0035 #include <KgvPageLayoutDia.h>
0036 #include <KgvPageLayoutHeader.h>
0037 #include <KgvPageLayoutSize.h>
0038 #include <KgvUnit.h>
0039 #include <KgvUnitWidgets.h>
0040 
0041 #include <QDebug>
0042 #include <QIcon>
0043 #include <QMessageBox>
0044 
0045 #include <qcheckbox.h>
0046 #include <qlabel.h>
0047 #include <qlayout.h>
0048 #include <qpainter.h>
0049 #include <qradiobutton.h>
0050 // Added by qt3to4:
0051 #include <QHBoxLayout>
0052 #include <QPaintEngine>
0053 #include <QPointer>
0054 #include <klocalizedstring.h>
0055 
0056 /******************************************************************/
0057 /* class KgvPagePreview                                            */
0058 /******************************************************************/
0059 
0060 /*===================== constructor ==============================*/
0061 KgvPagePreview::KgvPagePreview(QWidget *parent, const char *name, const KgvPageLayout &layout)
0062     : QGroupBox(i18n("Page Preview"), parent)
0063 {
0064     setObjectName(name);
0065     setPageLayout(layout);
0066     columns = 1;
0067     setMinimumSize(150, 150);
0068 }
0069 
0070 /*====================== destructor ==============================*/
0071 KgvPagePreview::~KgvPagePreview()
0072 {
0073 }
0074 
0075 /*=================== set layout =================================*/
0076 void KgvPagePreview::setPageLayout(const KgvPageLayout &layout)
0077 {
0078     // resolution[XY] is in pixel per pt
0079     double resolutionX = POINT_TO_INCH(static_cast<double>(KgvGlobal::dpiX()));
0080     double resolutionY = POINT_TO_INCH(static_cast<double>(KgvGlobal::dpiY()));
0081 
0082     m_pageWidth = layout.ptWidth * resolutionX;
0083     m_pageHeight = layout.ptHeight * resolutionY;
0084 
0085     double zh = 110.0 / m_pageHeight;
0086     double zw = 110.0 / m_pageWidth;
0087     double z = qMin(zw, zh);
0088 
0089     m_pageWidth *= z;
0090     m_pageHeight *= z;
0091 
0092     m_textFrameX = layout.ptLeft * resolutionX * z;
0093     m_textFrameY = layout.ptTop * resolutionY * z;
0094     m_textFrameWidth = m_pageWidth - (layout.ptLeft + layout.ptRight) * resolutionX * z;
0095     m_textFrameHeight = m_pageHeight - (layout.ptTop + layout.ptBottom) * resolutionY * z;
0096 
0097     qCDebug(KGRAPHVIEWERLIB_LOG) << "repaint in setPageLayout";
0098     repaint();
0099 }
0100 
0101 /*=================== set layout =================================*/
0102 void KgvPagePreview::setPageColumns(const KgvColumns &_columns)
0103 {
0104     columns = _columns.columns;
0105     repaint();
0106 }
0107 
0108 /*======================== draw contents =========================*/
0109 void KgvPagePreview::paintEvent(QPaintEvent *event)
0110 {
0111     QGroupBox::paintEvent(event);
0112     QPainter painter(this);
0113     painter.setRenderHint(QPainter::Antialiasing);
0114     double cw = m_textFrameWidth;
0115     if (columns != 1) {
0116         cw /= static_cast<double>(columns);
0117     }
0118     painter.setBrush(Qt::white);
0119     painter.setPen(QPen(Qt::black));
0120 
0121     int x = static_cast<int>((width() - m_pageWidth) * 0.5);
0122     int y = static_cast<int>((height() - m_pageHeight) * 0.5);
0123     int w = static_cast<int>(m_pageWidth);
0124     int h = static_cast<int>(m_pageHeight);
0125     // painter.drawRect( x + 1, y + 1, w, h);
0126     painter.drawRect(x, y, w, h);
0127 
0128     painter.setBrush(QBrush(Qt::black, Qt::HorPattern));
0129     if (m_textFrameWidth == m_pageWidth || m_textFrameHeight == m_pageHeight) {
0130         painter.setPen(Qt::NoPen);
0131     } else {
0132         painter.setPen(Qt::lightGray);
0133     }
0134 
0135     for (int i = 0; i < columns; ++i) {
0136         painter.drawRect(x + static_cast<int>(m_textFrameX) + static_cast<int>(i * cw), y + static_cast<int>(m_textFrameY), static_cast<int>(cw), static_cast<int>(m_textFrameHeight));
0137     }
0138 }
0139 
0140 /******************************************************************/
0141 /* class KgvPageLayoutDia                                          */
0142 /******************************************************************/
0143 
0144 /*==================== constructor ===============================*/
0145 KgvPageLayoutDia::KgvPageLayoutDia(QWidget *parent, KgvPageLayout &layout, int tabs, KgvUnit::Unit unit)
0146     : KPageDialog(parent)
0147     ,
0148     /*                                  : KDialogBase( KDialogBase::Tabbed, i18n("Page Layout"), KDialogBase::Ok | KDialogBase::Cancel,
0149                                       KDialogBase::Ok, parent, name, modal),*/
0150     m_layout(layout)
0151     , m_unit(unit)
0152     , flags(tabs)
0153     , m_pageSizeTab(nullptr)
0154     , m_columnsTab(nullptr)
0155     , m_headerTab(nullptr)
0156 
0157 {
0158     setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
0159 
0160     m_column.columns = 1;
0161 
0162     if (tabs & FORMAT_AND_BORDERS)
0163         setupTab1(true);
0164     //     if ( tabs & HEADER_AND_FOOTER ) setupTab2( hf );
0165 
0166     setFocusPolicy(Qt::StrongFocus);
0167     setFocus();
0168     // TODO: make validation query code in slotOk work, or rather port to KWarningMessage
0169 }
0170 
0171 /*==================== constructor ===============================*/
0172 KgvPageLayoutDia::KgvPageLayoutDia(QWidget *parent, KgvPageLayout &layout, const KgvColumns &columns, int tabs, KgvUnit::Unit unit)
0173     : KPageDialog(parent)
0174     ,
0175     /*                  : KDialogBase( KDialogBase::Tabbed, i18n("Page Layout"), KDialogBase::Ok | KDialogBase::Cancel,
0176                       KDialogBase::Ok, parent, name, true),*/
0177     m_layout(layout)
0178     , m_column(columns)
0179     , m_unit(unit)
0180     , flags(tabs)
0181     , m_pageSizeTab(nullptr)
0182     , m_columnsTab(nullptr)
0183     , m_headerTab(nullptr)
0184 {
0185     setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
0186 
0187     if (tabs & FORMAT_AND_BORDERS)
0188         setupTab1(!(tabs & DISABLE_BORDERS));
0189     //     if ( tabs & HEADER_AND_FOOTER ) setupTab2( hf );
0190     //     if ( tabs & COLUMNS ) setupTab3();
0191     //     if ( tabs & KW_HEADER_AND_FOOTER ) setupTab4(kwhf);
0192 
0193     setFocusPolicy(Qt::StrongFocus);
0194     setFocus();
0195 
0196     // TODO: make validation query code in slotOk work, or rather port to KWarningMessage
0197 }
0198 
0199 /*===================== destructor ===============================*/
0200 KgvPageLayoutDia::~KgvPageLayoutDia()
0201 {
0202 }
0203 
0204 /*======================= show dialog ============================*/
0205 bool KgvPageLayoutDia::pageLayout(KgvPageLayout &layout, KgvHeadFoot &hf, int tabs, KgvUnit::Unit &unit, QWidget *parent)
0206 {
0207     bool res = false;
0208     QPointer<KgvPageLayoutDia> dlg = new KgvPageLayoutDia(parent, layout, tabs, unit);
0209 
0210     if (dlg->exec() == QDialog::Accepted) {
0211         res = true;
0212         if (tabs & FORMAT_AND_BORDERS)
0213             layout = dlg->layout();
0214         if (tabs & HEADER_AND_FOOTER)
0215             hf = dlg->headFoot();
0216         unit = dlg->unit();
0217     }
0218 
0219     delete dlg;
0220 
0221     return res;
0222 }
0223 
0224 /*======================= show dialog ============================*/
0225 bool KgvPageLayoutDia::pageLayout(KgvPageLayout &layout, KgvHeadFoot &hf, KgvColumns &columns, KgvKWHeaderFooter &_kwhf, int tabs, KgvUnit::Unit &unit, QWidget *parent)
0226 {
0227     bool res = false;
0228     QPointer<KgvPageLayoutDia> dlg = new KgvPageLayoutDia(parent, layout, columns, tabs, unit);
0229 
0230     if (dlg->exec() == QDialog::Accepted) {
0231         res = true;
0232         if (tabs & FORMAT_AND_BORDERS)
0233             layout = dlg->layout();
0234         if (tabs & HEADER_AND_FOOTER)
0235             hf = dlg->headFoot();
0236         if (tabs & COLUMNS)
0237             columns = dlg->columns();
0238         if (tabs & KW_HEADER_AND_FOOTER)
0239             _kwhf = dlg->headerFooter();
0240         unit = dlg->unit();
0241     }
0242 
0243     delete dlg;
0244 
0245     return res;
0246 }
0247 
0248 /*===================== get a standard page layout ===============*/
0249 /*KgvPageLayout KgvPageLayoutDia::standardLayout()
0250 {
0251     return KgvPageLayout::standardLayout();
0252 }*/
0253 
0254 /*====================== get header - footer =====================*/
0255 KgvHeadFoot KgvPageLayoutDia::headFoot() const
0256 {
0257     KgvHeadFoot hf;
0258     hf.headLeft = eHeadLeft->text();
0259     hf.headMid = eHeadMid->text();
0260     hf.headRight = eHeadRight->text();
0261     hf.footLeft = eFootLeft->text();
0262     hf.footMid = eFootMid->text();
0263     hf.footRight = eFootRight->text();
0264     return hf;
0265 }
0266 
0267 /*================================================================*/
0268 const KgvKWHeaderFooter &KgvPageLayoutDia::headerFooter()
0269 {
0270     return m_headerTab->headerFooter();
0271 }
0272 
0273 /*================ setup page size & margins tab ==================*/
0274 void KgvPageLayoutDia::setupTab1(bool enableBorders)
0275 {
0276     m_pageSizeTab = new KgvPageLayoutSize(nullptr, m_layout, m_unit, m_column, !(flags & DISABLE_UNIT), enableBorders);
0277     addPage(m_pageSizeTab, i18n("Page Size & Margins"));
0278     connect(m_pageSizeTab, &KgvPageLayoutSize::propertyChange, this, &KgvPageLayoutDia::sizeUpdated);
0279 }
0280 
0281 void KgvPageLayoutDia::sizeUpdated(KgvPageLayout &layout)
0282 {
0283     m_layout.ptWidth = layout.ptWidth;
0284     m_layout.ptHeight = layout.ptHeight;
0285     m_layout.ptLeft = layout.ptLeft;
0286     m_layout.ptRight = layout.ptRight;
0287     m_layout.ptTop = layout.ptTop;
0288     m_layout.ptBottom = layout.ptBottom;
0289     m_layout.format = layout.format;
0290     m_layout.orientation = layout.orientation;
0291     if (m_columnsTab)
0292         m_columnsTab->setLayout(layout);
0293 }
0294 
0295 // /*================ setup header and footer tab ===================*/
0296 // void KgvPageLayoutDia::setupTab2( const KgvHeadFoot& hf )
0297 // {
0298 //     QWidget *tab2 = addPage(i18n( "H&eader && Footer" ));
0299 //     QGridLayout *grid2 = new QGridLayout( tab2, 7, 2, 0, KDialog::spacingHint() );
0300 //
0301 //     // ------------- header ---------------
0302 //     QGroupBox *gHead = new QGroupBox( 0, Qt::Vertical, i18n( "Head Line" ), tab2 );
0303 //     gHead->layout()->setSpacing(KDialog::spacingHint());
0304 //     gHead->layout()->setMargin(KDialog::marginHint());
0305 //     QGridLayout *headGrid = new QGridLayout( gHead->layout(), 2, 3 );
0306 //
0307 //     QLabel *lHeadLeft = new QLabel( i18n( "Left:" ), gHead );
0308 //     headGrid->addWidget( lHeadLeft, 0, 0 );
0309 //
0310 //     eHeadLeft = new KLineEdit( gHead );
0311 //     headGrid->addWidget( eHeadLeft, 1, 0 );
0312 //     eHeadLeft->setText( hf.headLeft );
0313 //
0314 //     QLabel *lHeadMid = new QLabel( i18n( "Mid:" ), gHead );
0315 //     headGrid->addWidget( lHeadMid, 0, 1 );
0316 //
0317 //     eHeadMid = new KLineEdit( gHead );
0318 //     headGrid->addWidget( eHeadMid, 1, 1 );
0319 //     eHeadMid->setText( hf.headMid );
0320 //
0321 //     QLabel *lHeadRight = new QLabel( i18n( "Right:" ), gHead );
0322 //     headGrid->addWidget( lHeadRight, 0, 2 );
0323 //
0324 //     eHeadRight = new KLineEdit( gHead );
0325 //     headGrid->addWidget( eHeadRight, 1, 2 );
0326 //     eHeadRight->setText( hf.headRight );
0327 //
0328 //     grid2->addMultiCellWidget( gHead, 0, 1, 0, 1 );
0329 //
0330 //     // ------------- footer ---------------
0331 //     QGroupBox *gFoot = new QGroupBox( 0, Qt::Vertical, i18n( "Foot Line" ), tab2 );
0332 //     gFoot->layout()->setSpacing(KDialog::spacingHint());
0333 //     gFoot->layout()->setMargin(KDialog::marginHint());
0334 //     QGridLayout *footGrid = new QGridLayout( gFoot->layout(), 2, 3 );
0335 //
0336 //     QLabel *lFootLeft = new QLabel( i18n( "Left:" ), gFoot );
0337 //     footGrid->addWidget( lFootLeft, 0, 0 );
0338 //
0339 //     eFootLeft = new KLineEdit( gFoot );
0340 //     footGrid->addWidget( eFootLeft, 1, 0 );
0341 //     eFootLeft->setText( hf.footLeft );
0342 //
0343 //     QLabel *lFootMid = new QLabel( i18n( "Mid:" ), gFoot );
0344 //     footGrid->addWidget( lFootMid, 0, 1 );
0345 //
0346 //     eFootMid = new KLineEdit( gFoot );
0347 //     footGrid->addWidget( eFootMid, 1, 1 );
0348 //     eFootMid->setText( hf.footMid );
0349 //
0350 //     QLabel *lFootRight = new QLabel( i18n( "Right:" ), gFoot );
0351 //     footGrid->addWidget( lFootRight, 0, 2 );
0352 //
0353 //     eFootRight = new KLineEdit( gFoot );
0354 //     footGrid->addWidget( eFootRight, 1, 2 );
0355 //     eFootRight->setText( hf.footRight );
0356 //
0357 //     grid2->addMultiCellWidget( gFoot, 2, 3, 0, 1 );
0358 //
0359 //     QLabel *lMacros2 = new QLabel( i18n( "You can insert several tags in the text:" ), tab2 );
0360 //     grid2->addMultiCellWidget( lMacros2, 4, 4, 0, 1 );
0361 //
0362 //     QLabel *lMacros3 = new QLabel( i18n("<qt><ul><li>&lt;sheet&gt; The sheet name</li>"
0363 //                            "<li>&lt;page&gt; The current page</li>"
0364 //                            "<li>&lt;pages&gt; The total number of pages</li>"
0365 //                            "<li>&lt;name&gt; The filename or URL</li>"
0366 //                            "<li>&lt;file&gt; The filename with complete path or the URL</li></ul></qt>"), tab2 );
0367 //     grid2->addMultiCellWidget( lMacros3, 5, 6, 0, 0, Qt::AlignTop );
0368 //
0369 //     QLabel *lMacros4 = new QLabel( i18n("<qt><ul><li>&lt;time&gt; The current time</li>"
0370 //                            "<li>&lt;date&gt; The current date</li>"
0371 //                            "<li>&lt;author&gt; Your full name</li>"
0372 //                            "<li>&lt;org&gt; Your organization</li>"
0373 //                            "<li>&lt;email&gt; Your email address</li></ul></qt>"), tab2 );
0374 //     grid2->addMultiCellWidget( lMacros4, 5, 6, 1, 1, Qt::AlignTop );
0375 // }
0376 //
0377 // /*================================================================*/
0378 // void KgvPageLayoutDia::setupTab3()
0379 // {
0380 //     QWidget *tab3 = addPage(i18n( "Col&umns" ));
0381 //     QHBoxLayout *lay = new QHBoxLayout(tab3);
0382 //     m_columnsTab = new KgvPageLayoutColumns(tab3, m_column, m_unit, m_layout);
0383 //     m_columnsTab->layout()->setMargin(0);
0384 //     lay->addWidget(m_columnsTab);
0385 //     m_columnsTab->show();
0386 //     connect (m_columnsTab, SIGNAL(propertyChange(KgvColumns&)),
0387 //             this, SLOT(columnsUpdated(KgvColumns&)));
0388 // }
0389 //
0390 // void KgvPageLayoutDia::columnsUpdated(KgvColumns &columns) {
0391 //     m_column.columns = columns.columns;
0392 //     m_column.ptColumnSpacing = columns.ptColumnSpacing;
0393 //     if(m_pageSizeTab)
0394 //         m_pageSizeTab->setColumns(columns);
0395 // }
0396 //
0397 // /*================================================================*/
0398 // void KgvPageLayoutDia::setupTab4(const KgvKWHeaderFooter kwhf )
0399 // {
0400 //     QWidget *tab4 = addPage(i18n( "H&eader && Footer" ));
0401 //     QHBoxLayout *lay = new QHBoxLayout(tab4);
0402 //     m_headerTab = new KgvPageLayoutHeader(tab4, m_unit, kwhf);
0403 //     m_headerTab->layout()->setMargin(0);
0404 //     lay->addWidget(m_headerTab);
0405 //     m_headerTab->show();
0406 //
0407 // }
0408 //
0409 
0410 /* Validation when closing. Error messages are never liked, but
0411   better let the users enter all values in any order, and have one
0412   final validation, than preventing them from entering values. */
0413 void KgvPageLayoutDia::slotOk()
0414 {
0415     if (m_pageSizeTab)
0416         m_pageSizeTab->queryClose();
0417     KPageDialog::accept(); // accept
0418 }
0419 
0420 #include "moc_KgvPageLayoutDia.cpp"