korganizer
navigatorbar.cpp
Go to the documentation of this file.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 "navigatorbar.h"
00026 #include "koglobals.h"
00027 #include "koprefs.h"
00028
00029 #include <kdebug.h>
00030 #include <kcalendarsystem.h>
00031 #include <klocale.h>
00032 #include <kglobal.h>
00033 #include <kiconloader.h>
00034
00035 #include <QHBoxLayout>
00036 #include <QLabel>
00037 #include <QMenu>
00038 #include <QMouseEvent>
00039 #include <QString>
00040 #include <QToolButton>
00041
00042 ActiveLabel::ActiveLabel( QWidget *parent ) : QLabel( parent )
00043 {
00044 }
00045
00046 void ActiveLabel::mouseReleaseEvent( QMouseEvent * )
00047 {
00048 emit clicked();
00049 }
00050
00051 NavigatorBar::NavigatorBar( QWidget *parent )
00052 : QWidget( parent ), mHasMinWidth( false )
00053 {
00054 QFont tfont = font();
00055 tfont.setPointSize( 10 );
00056 tfont.setBold( false );
00057
00058 bool isRTL = KOGlobals::self()->reverseLayout();
00059
00060 mPrevYear = createNavigationButton(
00061 isRTL ? "arrow-right-double" : "arrow-left-double",
00062 i18n( "Scroll backward to the previous year" ),
00063 i18n( "Click this button to scroll the display to the "
00064 "same approximate day of the previous year" ) );
00065
00066 mPrevMonth = createNavigationButton(
00067 isRTL ? "arrow-right" : "arrow-left",
00068 i18n( "Scroll backward to the previous month" ),
00069 i18n( "Click this button to scroll the display to the "
00070 "same approximate date of the previous month" ) );
00071
00072 mNextMonth = createNavigationButton(
00073 isRTL ? "arrow-left" : "arrow-right",
00074 i18n( "Scroll forward to the next month" ),
00075 i18n( "Click this button to scroll the display to the "
00076 "same approximate date of the next month" ) );
00077
00078 mNextYear = createNavigationButton(
00079 isRTL ? "arrow-left-double" : "arrow-right-double",
00080 i18n( "Scroll forward to the next year" ),
00081 i18n( "Click this button to scroll the display to the "
00082 "same approximate day of the next year" ) );
00083
00084
00085 mMonth = new ActiveLabel( this );
00086 mMonth->setFont( tfont );
00087 mMonth->setAlignment( Qt::AlignCenter );
00088 mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
00089 mMonth->setToolTip( i18n( "Select a month" ) );
00090
00091
00092 mYear = new ActiveLabel( this );
00093 mYear->setFont( tfont );
00094 mYear->setAlignment( Qt::AlignCenter );
00095 mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
00096 mYear->setToolTip( i18n( "Select a year" ) );
00097
00098
00099 QHBoxLayout *ctrlLayout = new QHBoxLayout( this );
00100 ctrlLayout->addWidget( mPrevYear );
00101 ctrlLayout->addWidget( mPrevMonth );
00102 ctrlLayout->addWidget( mMonth );
00103 ctrlLayout->addWidget( mYear );
00104 ctrlLayout->addWidget( mNextMonth );
00105 ctrlLayout->addWidget( mNextYear );
00106
00107 connect( mPrevYear, SIGNAL(clicked()), SIGNAL(goPrevYear()) );
00108 connect( mPrevMonth, SIGNAL(clicked()), SIGNAL(goPrevMonth()) );
00109 connect( mNextMonth, SIGNAL(clicked()), SIGNAL(goNextMonth()) );
00110 connect( mNextYear, SIGNAL(clicked()), SIGNAL(goNextYear()) );
00111 connect( mMonth, SIGNAL(clicked()), SLOT(selectMonth()) );
00112 connect( mYear, SIGNAL(clicked()), SLOT(selectYear()) );
00113 }
00114
00115 NavigatorBar::~NavigatorBar()
00116 {
00117 }
00118
00119 void NavigatorBar::showButtons( bool left, bool right )
00120 {
00121 if ( left ) {
00122 mPrevYear->show();
00123 mPrevMonth->show();
00124 } else {
00125 mPrevYear->hide();
00126 mPrevMonth->hide();
00127 }
00128
00129 if ( right ) {
00130 mNextYear->show();
00131 mNextMonth->show();
00132 } else {
00133 mNextYear->hide();
00134 mNextMonth->hide();
00135 }
00136 }
00137
00138 void NavigatorBar::selectDates( const KCal::DateList &dateList )
00139 {
00140 if ( dateList.count() > 0 ) {
00141 mDate = dateList.first();
00142
00143 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00144
00145 if ( !mHasMinWidth ) {
00146
00147 int i;
00148 int maxwidth = 0;
00149
00150 for ( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
00151 QString m = calSys->monthName( i, calSys->year( mDate ) );
00152 int w = QFontMetrics( mMonth->font() ).width( QString( "%1" ).arg( m ) );
00153 if ( w > maxwidth ) {
00154 maxwidth = w;
00155 }
00156 }
00157 mMonth->setMinimumWidth( maxwidth );
00158
00159 mHasMinWidth = true;
00160 }
00161
00162
00163 mMonth->setText( i18nc( "monthname", "%1", calSys->monthName( mDate ) ) );
00164 mYear->setText( i18nc( "4 digit year", "%1", calSys->yearString( mDate ) ) );
00165 }
00166 }
00167
00168 void NavigatorBar::selectMonth()
00169 {
00170
00171 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00172
00173 int i;
00174 int month = calSys->month( mDate );
00175 int year = calSys->year( mDate );
00176 int months = calSys->monthsInYear( mDate );
00177
00178 QMenu *menu = new QMenu( mMonth );
00179 QList<QAction *>act;
00180
00181 QAction *activateAction = 0;
00182 for ( i=1; i <= months; ++i ) {
00183 QAction *monthAction = menu->addAction( calSys->monthName( i, year ) );
00184 act.append( monthAction );
00185 if ( i == month ) {
00186 activateAction = monthAction;
00187 }
00188 }
00189 if ( activateAction ) {
00190 menu->setActiveAction( activateAction );
00191 }
00192 QAction *selectedAct = menu->exec( mMonth->mapToGlobal( QPoint( 0, 0 ) ) );
00193 if ( selectedAct && ( selectedAct != activateAction ) ) {
00194 for ( i=0; i < months; i++ ) {
00195 if ( act[i] == selectedAct ) {
00196 emit goMonth( i + 1 );
00197 }
00198 }
00199 }
00200 qDeleteAll( act );
00201 act.clear();
00202 delete menu;
00203 }
00204
00205 void NavigatorBar::selectYear()
00206 {
00207 const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
00208
00209 int i;
00210 int year = calSys->year( mDate );
00211 int years = 11;
00212 int minYear = year - ( years / 3 );
00213
00214 QMenu *menu = new QMenu( mYear );
00215 QList<QAction *>act;
00216
00217 QString yearStr;
00218 QAction *activateAction = 0;
00219 int y = minYear;
00220 for ( i=0; i < years; i++ ) {
00221 QAction *yearAction = menu->addAction( yearStr.setNum( y ) );
00222 act.append( yearAction );
00223 if ( y == year ) {
00224 activateAction = yearAction;
00225 }
00226 y++;
00227 }
00228 if ( activateAction ) {
00229 menu->setActiveAction( activateAction );
00230 }
00231 QAction *selectedAct = menu->exec( mYear->mapToGlobal( QPoint( 0, 0 ) ) );
00232 if ( selectedAct && ( selectedAct != activateAction ) ) {
00233 int y = minYear;
00234 for ( i=0; i < years; ++i ) {
00235 if ( act[i] == selectedAct ) {
00236 emit goYear( y );
00237 }
00238 y++;
00239 }
00240 }
00241 qDeleteAll( act );
00242 act.clear();
00243 delete menu;
00244 }
00245
00246 QToolButton *NavigatorBar::createNavigationButton( const QString &icon,
00247 const QString &toolTip,
00248 const QString &whatsThis )
00249 {
00250 QToolButton *button = new QToolButton( this );
00251
00252 button->setIcon(
00253 KIconLoader::global()->loadIcon( icon, KIconLoader::Desktop, KIconLoader::SizeSmall ) );
00254 button->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
00255 button->setToolButtonStyle( Qt::ToolButtonIconOnly );
00256 button->setToolTip( toolTip );
00257 button->setWhatsThis( whatsThis );
00258
00259 return button;
00260 }
00261
00262 #include "navigatorbar.moc"