00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qstring.h>
00026 #include <qtooltip.h>
00027 #include <qpushbutton.h>
00028 #include <qlayout.h>
00029 #include <qframe.h>
00030 #include <qpopupmenu.h>
00031 #include <qlabel.h>
00032
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <kglobal.h>
00036 #include <kiconloader.h>
00037
00038 #include "koglobals.h"
00039 #include "koprefs.h"
00040
00041 #include <kcalendarsystem.h>
00042
00043 #include "navigatorbar.h"
00044
00045 ActiveLabel::ActiveLabel( QWidget *parent, const char *name )
00046 : QLabel( parent, name )
00047 {
00048 }
00049
00050 void ActiveLabel::mouseReleaseEvent( QMouseEvent * )
00051 {
00052 emit clicked();
00053 }
00054
00055
00056 NavigatorBar::NavigatorBar( QWidget *parent, const char *name )
00057 : QWidget( parent, name ), mHasMinWidth( false )
00058 {
00059 QFont tfont = font();
00060 tfont.setPointSize( 10 );
00061 tfont.setBold( false );
00062
00063 bool isRTL = KOGlobals::self()->reverseLayout();
00064
00065 QPixmap pix;
00066
00067 pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
00068 mPrevYear = new QPushButton( this );
00069 mPrevYear->setPixmap( pix );
00070 mPrevYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00071 QToolTip::add( mPrevYear, i18n( "Previous year" ) );
00072
00073 pix = KOGlobals::self()->smallIcon( isRTL ? "1rightarrow" : "1leftarrow");
00074 mPrevMonth = new QPushButton( this );
00075 mPrevMonth->setPixmap( pix );
00076 mPrevMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00077 QToolTip::add( mPrevMonth, i18n( "Previous month" ) );
00078
00079
00080 pix = KOGlobals::self()->smallIcon( isRTL ? "1leftarrow" : "1rightarrow");
00081 mNextMonth = new QPushButton( this );
00082 mNextMonth->setPixmap( pix );
00083 mNextMonth->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00084 QToolTip::add( mNextMonth, i18n( "Next month" ) );
00085
00086 pix = KOGlobals::self()->smallIcon( isRTL ? "2leftarrow" : "2rightarrow");
00087 mNextYear = new QPushButton( this );
00088 mNextYear->setPixmap( pix );
00089 mNextYear->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
00090 QToolTip::add( mNextYear, i18n( "Next year" ) );
00091
00092
00093 mMonth = new ActiveLabel( this );
00094 mMonth->setFont( tfont );
00095 mMonth->setAlignment( AlignCenter );
00096 mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
00097 QToolTip::add( mMonth, i18n( "Select a month" ) );
00098
00099
00100 mYear = new ActiveLabel( this );
00101 mYear->setFont( tfont );
00102 mYear->setAlignment( AlignCenter );
00103 mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
00104 QToolTip::add( mYear, i18n( "Select a year" ) );
00105
00106
00107 QBoxLayout *ctrlLayout = new QHBoxLayout( this, 0, 4 );
00108 ctrlLayout->addWidget( mPrevYear );
00109 ctrlLayout->addWidget( mPrevMonth );
00110 QBoxLayout *dLayout = new QHBoxLayout( this );
00111 dLayout->insertStretch( 0, 50 );
00112 dLayout->addWidget( mMonth );
00113 dLayout->addWidget( mYear );
00114 dLayout->addStretch( 50 );
00115 ctrlLayout->addLayout( dLayout );
00116 ctrlLayout->addWidget( mNextMonth );
00117 ctrlLayout->addWidget( mNextYear );
00118
00119 connect( mPrevYear, SIGNAL( clicked() ), SIGNAL( prevYearClicked() ) );
00120 connect( mPrevMonth, SIGNAL( clicked() ), SIGNAL( prevMonthClicked() ) );
00121 connect( mNextMonth, SIGNAL( clicked() ), SIGNAL( nextMonthClicked() ) );
00122 connect( mNextYear, SIGNAL( clicked() ), SIGNAL( nextYearClicked() ) );
00123 connect( mMonth, SIGNAL( clicked() ), SLOT( selectMonthFromMenu() ) );
00124 connect( mYear, SIGNAL( clicked() ), SLOT( selectYearFromMenu() ) );
00125 }
00126
00127 NavigatorBar::~NavigatorBar()
00128 {
00129 }
00130
00131 void NavigatorBar::showButtons( bool left, bool right )
00132 {
00133 if ( left ) {
00134 mPrevYear->show();
00135 mPrevMonth->show();
00136 } else {
00137 mPrevYear->hide();
00138 mPrevMonth->hide();
00139 }
00140
00141 if ( right ) {
00142 mNextYear->show();
00143 mNextMonth->show();
00144 } else {
00145 mNextYear->hide();
00146 mNextMonth->hide();
00147 }
00148
00149 }
00150
00151 void NavigatorBar::selectDates( const KCal::DateList &dateList )
00152 {
00153 if ( dateList.count() > 0 ) {
00154 mDate = dateList.first();
00155
00156 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00157
00158
00159 int i;
00160 int maxwidth = 0;
00161
00162 for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00163 int w = QFontMetrics( mMonth->font() ).
00164 width( QString( "%1" ).
00165 arg( calSys->monthName( i, calSys->year( mDate ) ) ) );
00166 if ( w > maxwidth ) {
00167 maxwidth = w;
00168 }
00169 }
00170 mMonth->setMinimumWidth( maxwidth );
00171
00172 mHasMinWidth = true;
00173
00174
00175 mMonth->setText( i18n( "monthname", "%1" ).arg( calSys->monthName( mDate ) ) );
00176 mYear->setText( i18n( "4 digit year", "%1" ).arg( calSys->yearString( mDate, false ) ) );
00177 }
00178 }
00179
00180 void NavigatorBar::selectMonthFromMenu()
00181 {
00182
00183 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00184
00185 int i, month, months = calSys->monthsInYear( mDate );
00186
00187 QPopupMenu *popup = new QPopupMenu( mMonth );
00188
00189 for ( i = 1; i <= months; i++ )
00190 popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
00191
00192 popup->setActiveItem( calSys->month( mDate ) - 1 );
00193 popup->setMinimumWidth( mMonth->width() );
00194
00195 if ( ( month = popup->exec( mMonth->mapToGlobal( QPoint( 0, 0 ) ),
00196 calSys->month( mDate ) - 1 ) ) == -1 ) {
00197 delete popup;
00198 return;
00199 }
00200
00201 emit monthSelected( month );
00202
00203 delete popup;
00204 }
00205
00206 void NavigatorBar::selectYearFromMenu()
00207 {
00208 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00209
00210 int year = calSys->year( mDate );
00211 int years = 11;
00212 int minYear = year - ( years / 3 );
00213
00214 QPopupMenu *popup = new QPopupMenu( mYear );
00215
00216 QString yearStr;
00217 int y = minYear;
00218 for ( int i=0; i < years; i++ ) {
00219 popup->insertItem( yearStr.setNum( y ), i );
00220 y++;
00221 }
00222 popup->setActiveItem( year - minYear );
00223
00224 if ( ( year = popup->exec( mYear->mapToGlobal( QPoint( 0, 0 ) ),
00225 year - minYear ) ) == -1 ) {
00226 delete popup;
00227 return;
00228 }
00229
00230 emit yearSelected( year + minYear );
00231
00232 delete popup;
00233 }
00234
00235 #include "navigatorbar.moc"