korganizer

navigatorbar.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqstring.h>
26 #include <tqtooltip.h>
27 #include <tqpushbutton.h>
28 #include <tqlayout.h>
29 #include <tqframe.h>
30 #include <tqpopupmenu.h>
31 #include <tqlabel.h>
32 
33 #include <kdebug.h>
34 #include <tdelocale.h>
35 #include <tdeglobal.h>
36 #include <kiconloader.h>
37 
38 #include "koglobals.h"
39 #include "koprefs.h"
40 
41 #include <kcalendarsystem.h>
42 
43 #include "navigatorbar.h"
44 
45 ActiveLabel::ActiveLabel( TQWidget *parent, const char *name )
46  : TQLabel( parent, name )
47 {
48 }
49 
50 void ActiveLabel::mouseReleaseEvent( TQMouseEvent * )
51 {
52  emit clicked();
53 }
54 
55 
56 NavigatorBar::NavigatorBar( TQWidget *parent, const char *name )
57  : TQWidget( parent, name ), mHasMinWidth( false )
58 {
59  TQFont tfont = font();
60  tfont.setPointSize( 10 );
61  tfont.setBold( false );
62 
63  // Create a horizontal spacers
64  TQSpacerItem *frontSpacer = new TQSpacerItem( 50, 1, TQSizePolicy::Expanding );
65  TQSpacerItem *endSpacer = new TQSpacerItem( 50, 1, TQSizePolicy::Expanding );
66 
67  bool isRTL = KOGlobals::self()->reverseLayout();
68 
69  TQPixmap pix;
70  // Create backward navigation buttons
71  pix = KOGlobals::self()->smallIcon( isRTL ? "2rightarrow" : "2leftarrow" );
72  mPrevYear = new TQPushButton( this );
73  mPrevYear->setPixmap( pix );
74  mPrevYear->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
75  TQToolTip::add( mPrevYear, i18n( "Previous year" ) );
76 
77  pix = KOGlobals::self()->smallIcon( isRTL ? "1rightarrow" : "1leftarrow");
78  mPrevMonth = new TQPushButton( this );
79  mPrevMonth->setPixmap( pix );
80  mPrevMonth->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
81  TQToolTip::add( mPrevMonth, i18n( "Previous month" ) );
82 
83  // Create forward navigation buttons
84  pix = KOGlobals::self()->smallIcon( isRTL ? "1leftarrow" : "1rightarrow");
85  mNextMonth = new TQPushButton( this );
86  mNextMonth->setPixmap( pix );
87  mNextMonth->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
88  TQToolTip::add( mNextMonth, i18n( "Next month" ) );
89 
90  pix = KOGlobals::self()->smallIcon( isRTL ? "2leftarrow" : "2rightarrow");
91  mNextYear = new TQPushButton( this );
92  mNextYear->setPixmap( pix );
93  mNextYear->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
94  TQToolTip::add( mNextYear, i18n( "Next year" ) );
95 
96  // Create month name button
97  mMonth = new ActiveLabel( this );
98  mMonth->setFont( tfont );
99  mMonth->setAlignment( AlignCenter );
100  mMonth->setMinimumHeight( mPrevYear->sizeHint().height() );
101  TQToolTip::add( mMonth, i18n( "Select a month" ) );
102 
103  // Create year button
104  mYear = new ActiveLabel( this );
105  mYear->setFont( tfont );
106  mYear->setAlignment( AlignCenter );
107  mYear->setMinimumHeight( mPrevYear->sizeHint().height() );
108  TQToolTip::add( mYear, i18n( "Select a year" ) );
109 
110  // set up control frame layout
111  TQHBoxLayout *ctrlLayout = new TQHBoxLayout( this );
112  ctrlLayout->addWidget( mPrevYear );
113  ctrlLayout->addWidget( mPrevMonth );
114  ctrlLayout->addItem( frontSpacer );
115  ctrlLayout->addWidget( mMonth );
116  ctrlLayout->addWidget( mYear );
117  ctrlLayout->addItem( endSpacer );
118  ctrlLayout->addWidget( mNextMonth );
119  ctrlLayout->addWidget( mNextYear );
120 
121  connect( mPrevYear, TQ_SIGNAL( clicked() ), TQ_SIGNAL( prevYearClicked() ) );
122  connect( mPrevMonth, TQ_SIGNAL( clicked() ), TQ_SIGNAL( prevMonthClicked() ) );
123  connect( mNextMonth, TQ_SIGNAL( clicked() ), TQ_SIGNAL( nextMonthClicked() ) );
124  connect( mNextYear, TQ_SIGNAL( clicked() ), TQ_SIGNAL( nextYearClicked() ) );
125  connect( mMonth, TQ_SIGNAL( clicked() ), TQ_SLOT( selectMonthFromMenu() ) );
126  connect( mYear, TQ_SIGNAL( clicked() ), TQ_SLOT( selectYearFromMenu() ) );
127 }
128 
129 NavigatorBar::~NavigatorBar()
130 {
131 }
132 
133 void NavigatorBar::showButtons( bool left, bool right )
134 {
135  if ( left ) {
136  mPrevYear->show();
137  mPrevMonth->show();
138  } else {
139  mPrevYear->hide();
140  mPrevMonth->hide();
141  }
142 
143  if ( right ) {
144  mNextYear->show();
145  mNextMonth->show();
146  } else {
147  mNextYear->hide();
148  mNextMonth->hide();
149  }
150 
151 }
152 
153 void NavigatorBar::selectDates( const KCal::DateList &dateList )
154 {
155  if ( dateList.count() > 0 ) {
156  mDate = dateList.first();
157 
158  const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
159 
160  // Set minimum width to width of widest month name label
161  int i;
162  int maxwidth = 0;
163 
164  for( i = 1; i <= calSys->monthsInYear( mDate ); ++i ) {
165  int w = TQFontMetrics( mMonth->font() ).
166  width( TQString( "%1" ).
167  arg( calSys->monthName( i, calSys->year( mDate ) ) ) );
168  if ( w > maxwidth ) {
169  maxwidth = w;
170  }
171  }
172  mMonth->setMinimumWidth( maxwidth );
173 
174  mHasMinWidth = true;
175 
176  // set the label text at the top of the navigator
177  mMonth->setText( i18n( "monthname", "%1" ).arg( calSys->monthName( mDate ) ) );
178  mYear->setText( i18n( "4 digit year", "%1" ).arg( calSys->yearString( mDate, false ) ) );
179  }
180 }
181 
182 void NavigatorBar::selectMonthFromMenu()
183 {
184  // every year can have different month names (in some calendar systems)
185  const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
186 
187  int i, month, months = calSys->monthsInYear( mDate );
188 
189  TQPopupMenu *popup = new TQPopupMenu( mMonth );
190 
191  for ( i = 1; i <= months; i++ )
192  popup->insertItem( calSys->monthName( i, calSys->year( mDate ) ), i );
193 
194  popup->setActiveItem( calSys->month( mDate ) - 1 );
195  popup->setMinimumWidth( mMonth->width() );
196 
197  if ( ( month = popup->exec( mMonth->mapToGlobal( TQPoint( 0, 0 ) ),
198  calSys->month( mDate ) - 1 ) ) == -1 ) {
199  delete popup;
200  return; // canceled
201  }
202 
203  emit monthSelected( month );
204 
205  delete popup;
206 }
207 
208 void NavigatorBar::selectYearFromMenu()
209 {
210  const KCalendarSystem *calSys = KOGlobals::self()->calendarSystem();
211 
212  int year = calSys->year( mDate );
213  int years = 11; // odd number (show a few years ago -> a few years from now)
214  int minYear = year - ( years / 3 );
215 
216  TQPopupMenu *popup = new TQPopupMenu( mYear );
217 
218  TQString yearStr;
219  int y = minYear;
220  for ( int i=0; i < years; i++ ) {
221  popup->insertItem( yearStr.setNum( y ), i );
222  y++;
223  }
224  popup->setActiveItem( year - minYear );
225 
226  if ( ( year = popup->exec( mYear->mapToGlobal( TQPoint( 0, 0 ) ),
227  year - minYear ) ) == -1 ) {
228  delete popup;
229  return; // canceled
230  }
231 
232  emit yearSelected( year + minYear );
233 
234  delete popup;
235 }
236 
237 #include "navigatorbar.moc"