Back to index

scribus-ng  1.3.4.dfsg+svn20071115
pageselector.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "pageselector.h"
00008 #include "pageselector.moc"
00009 
00010 #include <qvariant.h>
00011 #include <qcombobox.h>
00012 #include <qlineedit.h>
00013 #include <qpopupmenu.h>
00014 #if OPTION_USE_QTOOLBUTTON
00015     #include <qtoolbutton.h>
00016 #else
00017     #include <qpushbutton.h>
00018 #endif
00019 #include <qlayout.h>
00020 #include <qtoolbutton.h>
00021 #include <qtooltip.h>
00022 #include <qwhatsthis.h>
00023 #include <qimage.h>
00024 #include <qpixmap.h>
00025 #include "sccombobox.h"
00026 
00027 extern QPixmap loadIcon(QString nam);
00028 
00029 class PageValidator : public QValidator {
00030 public:
00031        PageValidator(int min, int max, QObject * parent);
00032        void fixup(QString & input) const;
00033        State validate(QString & input, int & pos) const;
00034 private:
00035        QRegExp rx;
00036        QRegExp rx2;
00037        PageSelector * pageSelector;
00038 };
00039 
00040 PageValidator::PageValidator(int /* min */, int /* max */, QObject * parent) : QValidator
00041 (parent), rx("^([0-9]+).*"), rx2("^[0-9]+$") 
00042 {
00043        pageSelector = static_cast<PageSelector*>(parent);
00044 }
00045 
00046 QValidator::State PageValidator::validate(QString & input, int & /* pos */) const
00047 {
00048        if (rx2.search(input) == 0 && pageSelector->PageCombo->text(input.toInt()-1) == input)
00049               return Acceptable;
00050        else
00051               return Intermediate;
00052 }
00053 
00054 void PageValidator::fixup(QString & input) const
00055 {
00056        if (rx.search(input) == 0)
00057               input = const_cast<QRegExp &>(rx).cap(1);
00058 }
00059        
00060 
00061 PageSelector::PageSelector( QWidget* parent, int maxPg ) : QWidget( parent, "pgsel", 0 )
00062 {
00063        LastPG = maxPg;
00064        APage = 1;
00065        PageSelectorLayout = new QHBoxLayout( this, 0, 1, "PageSelectorLayout");
00066 
00067 #if OPTION_USE_QTOOLBUTTON
00068        Start = new QToolButton( this, "Start" );
00069        Start->setAutoRaise(OPTION_FLAT_BUTTON);
00070        Back = new QToolButton( this, "Back" );
00071        Back->setAutoRaise(OPTION_FLAT_BUTTON);
00072        Forward = new QToolButton( this, "Forward" );
00073        Forward->setAutoRaise(OPTION_FLAT_BUTTON);
00074        Last = new QToolButton( this, "Last" );
00075        Last->setAutoRaise(OPTION_FLAT_BUTTON);
00076 #else
00077        Start = new QPushButton( this, "Start" );
00078        Start->setDefault( false );
00079        Start->setAutoDefault( false );
00080        Start->setFlat(OPTION_FLAT_BUTTON);
00081        Back = new QPushButton( this, "Back" );
00082        Back->setDefault( false );
00083        Back->setAutoDefault( false );
00084        Back->setFlat(OPTION_FLAT_BUTTON);
00085        Forward = new QPushButton( this, "Forward" );
00086        Forward->setDefault( false );
00087        Forward->setAutoDefault( false );
00088        Forward->setFlat(OPTION_FLAT_BUTTON);
00089        Last = new QPushButton( this, "Last" );
00090        Last->setDefault( false );
00091        Last->setAutoDefault( false );
00092        Last->setFlat(OPTION_FLAT_BUTTON);
00093 #endif
00094        Start->setPixmap( loadIcon("16/go-first.png") );
00095        Start->setFocusPolicy(QWidget::NoFocus);
00096        PageSelectorLayout->addWidget( Start );
00097 
00098        Back->setPixmap( loadIcon("16/go-previous.png") );
00099        Back->setFocusPolicy(QWidget::NoFocus);
00100        Back->setAutoRepeat(true);
00101        PageSelectorLayout->addWidget( Back );
00102 
00103        v = new PageValidator(1, LastPG, this);
00104        PageCombo = new ScComboBox( true, this, "PageCombo" );
00105        PageCombo->setDuplicatesEnabled( false );
00106        PageCombo->lineEdit()->setAlignment(Qt::AlignHCenter);
00107        QString tmp;
00108        for (int a = 0; a < LastPG; ++a)
00109        {
00110               PageCombo->insertItem(tmp.setNum(a+1));
00111        }
00112        PageCombo->setValidator(v);
00113        PageCombo->setMinimumSize(fontMetrics().width( "999 of 999" )+20, 20);
00114        PageCombo->setFocusPolicy(QWidget::ClickFocus);
00115        PageSelectorLayout->addWidget( PageCombo );
00116        
00117        Forward->setPixmap( loadIcon("16/go-next.png") );
00118        Forward->setFocusPolicy(QWidget::NoFocus);
00119        Forward->setAutoRepeat(true);
00120        PageSelectorLayout->addWidget( Forward );
00121 
00122        Last->setPixmap( loadIcon("16/go-last.png") );
00123        Last->setFocusPolicy(QWidget::NoFocus);
00124        PageSelectorLayout->addWidget( Last );
00125        Forward->setEnabled(true);
00126        Last->setEnabled(true);
00127        Back->setEnabled(false);
00128        Start->setEnabled(false);
00129        if (APage == LastPG)
00130        {
00131               Forward->setEnabled(false);
00132               Last->setEnabled(false);
00133        }
00134 
00135        languageChange();
00136        // signals and slots connections
00137        connect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00138        connect( Back, SIGNAL( clicked() ), this, SLOT( goBk() ) );
00139        connect( Start, SIGNAL( clicked() ), this, SLOT( ToStart() ) );
00140        connect( Forward, SIGNAL( clicked() ), this, SLOT( goFw() ) );
00141        connect( Last, SIGNAL( clicked() ), this, SLOT( ToEnd() ) );
00142 }
00143 
00144 bool PageSelector::hasFocus()
00145 {
00146        return PageCombo->hasFocus();
00147 }
00148 
00149 
00150 void PageSelector::focusPolicy(QWidget::FocusPolicy policy)
00151 {
00152        PageCombo->setFocusPolicy(policy);
00153 }
00154 
00155 
00156 void PageSelector::GotoPgE(int a)
00157 {
00158        GotoPg(a);
00159        emit GotoPage(a+1);
00160 }
00161 
00162 
00163 void PageSelector::GotoPage()
00164 {
00165        static QRegExp rx("^([0-9])+.*");
00166        int p = rx.cap(1).toInt();
00167        if (p < 1)
00168               p=1;
00169        if (p > LastPG)
00170               p = LastPG;
00171        GotoPg(p-1);
00172        emit GotoPage(p);
00173 }
00174 
00175 
00176 void PageSelector::GotoPg(int a)
00177 {
00178        disconnect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00179        PageCombo->setCurrentItem(a);
00180        PageCombo->setEditText( tr( "%1 of %2" ).arg(a+1).arg(LastPG) );
00181        APage = a+1;
00182        Back->setEnabled(true);
00183        Start->setEnabled(true);
00184        Forward->setEnabled(true);
00185        Last->setEnabled(true);
00186        if (a == 0)
00187        {
00188               Back->setEnabled(false);
00189               Start->setEnabled(false);
00190        }
00191        if (a == LastPG-1)
00192        {
00193               Forward->setEnabled(false);
00194               Last->setEnabled(false);
00195        }
00196        connect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00197 }
00198 
00199 void PageSelector::setMaxValue(int a)
00200 {
00201        disconnect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00202        PageCombo->clear();
00203        LastPG = a;
00204        QString tmp;
00205 //     v->setTop(LastPG);
00206        for (int b = 0; b < LastPG; ++b)
00207        {
00208               PageCombo->insertItem(tmp.setNum(b+1));
00209        }
00210        PageCombo->setEditText( tr( "%1 of %2" ).arg(APage).arg(LastPG) );
00211        connect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00212 }
00213 
00214 void PageSelector::ToStart()
00215 {
00216        if (APage == 1)
00217               return;
00218        GotoPgE(0);
00219 }
00220 
00221 void PageSelector::ToEnd()
00222 {
00223        if (APage == LastPG)
00224               return;
00225        GotoPgE(LastPG-1);
00226 }
00227 
00228 void PageSelector::goBk()
00229 {
00230        APage--;
00231        if (APage < 1)
00232               APage = 1;
00233        GotoPgE(APage-1);
00234 }
00235 
00236 void PageSelector::goFw()
00237 {
00238        APage++;
00239        if (APage > LastPG)
00240               APage = LastPG;
00241        GotoPgE(APage-1);
00242 }
00243 
00244 void PageSelector::languageChange()
00245 {
00246        disconnect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00247        PageCombo->setEditText( tr( "%1 of %2").arg(APage).arg(LastPG) );
00248        connect( PageCombo, SIGNAL( activated(int) ), this, SLOT( GotoPgE(int) ) );
00249 }
00250 
00251 void PageSelector::clearFocus()
00252 {
00253        PageCombo->clearFocus();    
00254 }