korganizer

timelabels.cpp
1 /*
2  This file is part of KOrganizer.
3  Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
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 "timelabels.h"
26 
27 #include <tqhbox.h>
28 #include <tqvbox.h>
29 #include <tqlabel.h>
30 #include <tqframe.h>
31 #include <tqlayout.h>
32 #include <tqfont.h>
33 #include <tqfontmetrics.h>
34 #include <tqpainter.h>
35 #include <tqstringlist.h>
36 #include <tqdatetime.h>
37 
38 #include <tdeglobal.h>
39 
40 #include "koglobals.h"
41 #include "kocore.h"
42 #include "koprefs.h"
43 #include "koagenda.h"
44 
45 TimeLabels::TimeLabels(int rows,TQWidget *parent,const char *name,WFlags f) :
46  TQScrollView(parent,name,f)
47 {
48  mRows = rows;
49  mMiniWidth = 0;
50  mAgenda = 0;
51 
52  mCellHeight = KOPrefs::instance()->mHourSize*4;
53 
54  enableClipper(true);
55 
56  setHScrollBarMode(AlwaysOff);
57  setVScrollBarMode(AlwaysOff);
58 
59  resizeContents(50, int(mRows * mCellHeight) );
60 
61  viewport()->setBackgroundMode( PaletteBackground );
62 
63  mMousePos = new TQFrame(this);
64  mMousePos->setLineWidth(0);
65  mMousePos->setMargin(0);
66  mMousePos->setBackgroundColor(TQt::red);
67  mMousePos->setFixedSize(width(), 1);
68  addChild(mMousePos, 0, 0);
69 }
70 
71 void TimeLabels::mousePosChanged(const TQPoint &pos)
72 {
73  moveChild(mMousePos, 0, pos.y());
74 }
75 
76 void TimeLabels::showMousePos()
77 {
78  mMousePos->show();
79 }
80 
81 void TimeLabels::hideMousePos()
82 {
83  mMousePos->hide();
84 }
85 
86 void TimeLabels::setCellHeight(double height)
87 {
88  mCellHeight = height;
89 }
90 
91 /*
92  Optimization so that only the "dirty" portion of the scroll view
93  is redrawn. Unfortunately, this is not called by default paintEvent() method.
94 */
95 void TimeLabels::drawContents(TQPainter *p,int cx, int cy, int cw, int ch)
96 {
97  // bug: the parameters cx and cw are the areas that need to be
98  // redrawn, not the area of the widget. unfortunately, this
99  // code assumes the latter...
100 
101  // now, for a workaround...
102  cx = contentsX() + frameWidth()*2;
103  cw = contentsWidth() ;
104  // end of workaround
105 
106  int cell = ((int)(cy/mCellHeight));
107  double y = cell * mCellHeight;
108  TQFontMetrics fm = fontMetrics();
109  TQString hour;
110  TQString suffix = "am";
111  int timeHeight = fm.ascent();
112  TQFont nFont = font();
113  p->setFont( font() );
114 
115  if (!TDEGlobal::locale()->use12Clock()) {
116  suffix = "00";
117  } else
118  if (cell > 11) suffix = "pm";
119 
120  if ( timeHeight > mCellHeight ) {
121  timeHeight = int(mCellHeight-1);
122  int pointS = nFont.pointSize();
123  while ( pointS > 4 ) {
124  nFont.setPointSize( pointS );
125  fm = TQFontMetrics( nFont );
126  if ( fm.ascent() < mCellHeight )
127  break;
128  -- pointS;
129  }
130  fm = TQFontMetrics( nFont );
131  timeHeight = fm.ascent();
132  }
133  //timeHeight -= (timeHeight/4-2);
134  TQFont sFont = nFont;
135  sFont.setPointSize( sFont.pointSize()/2 );
136  TQFontMetrics fmS( sFont );
137  int startW = mMiniWidth - frameWidth()-2 ;
138  int tw2 = fmS.width(suffix);
139  int divTimeHeight = (timeHeight-1) /2 - 1;
140  //testline
141  //p->drawLine(0,0,0,contentsHeight());
142  while (y < cy + ch+mCellHeight) {
143  // hour, full line
144  p->drawLine( cx, int(y), cw+2, int(y) );
145  hour.setNum(cell);
146  // handle 24h and am/pm time formats
147  if (TDEGlobal::locale()->use12Clock()) {
148  if (cell == 12) suffix = "pm";
149  if (cell == 0) hour.setNum(12);
150  if (cell > 12) hour.setNum(cell - 12);
151  }
152 
153  // center and draw the time label
154  int timeWidth = fm.width(hour);
155  int offset = startW - timeWidth - tw2 -1 ;
156  p->setFont( nFont );
157  p->drawText( offset, int(y+timeHeight), hour);
158  p->setFont( sFont );
159  offset = startW - tw2;
160  p->drawText( offset, int(y+timeHeight-divTimeHeight), suffix);
161 
162  // increment indices
163  y += mCellHeight;
164  cell++;
165  }
166 
167 }
168 
172 int TimeLabels::minimumWidth() const
173 {
174  return mMiniWidth;
175 }
176 
178 void TimeLabels::updateConfig()
179 {
180  // Avoid crash on exit
181  if ( !mAgenda ) {
182  return;
183  }
184 
185  setFont(KOPrefs::instance()->mTimeBarFont);
186 
187  TQString test = "20";
188  if ( TDEGlobal::locale()->use12Clock() )
189  test = "12";
190  mMiniWidth = fontMetrics().width( test );
191  if ( TDEGlobal::locale()->use12Clock() )
192  test = "pm";
193  else {
194  test = "00";
195  }
196  TQFont sFont = font();
197  sFont.setPointSize( sFont.pointSize()/2 );
198  TQFontMetrics fmS( sFont );
199  mMiniWidth += fmS.width( test ) + frameWidth()*2+4 ;
200  // update geometry restrictions based on new settings
201  setFixedWidth( mMiniWidth );
202 
203  // update HourSize
204  mCellHeight = KOPrefs::instance()->mHourSize*4;
205  // If the agenda is zoomed out so that more then 24 would be shown,
206  // the agenda only shows 24 hours, so we need to take the cell height
207  // from the agenda, which is larger than the configured one!
208  if ( mAgenda && mCellHeight < 4*mAgenda->gridSpacingY() ) {
209  mCellHeight = 4*mAgenda->gridSpacingY();
210  }
211  resizeContents( mMiniWidth, int(mRows * mCellHeight+1) );
212 }
213 
215 void TimeLabels::positionChanged()
216 {
217  if ( mAgenda ) {
218  int adjustment = mAgenda->contentsY();
219  setContentsPos( 0, adjustment );
220  }
221 }
222 
223 void TimeLabels::positionChanged( int pos )
224 {
225  setContentsPos( 0, pos );
226 }
227 
229 void TimeLabels::setAgenda( KOAgenda* agenda )
230 {
231  mAgenda = agenda;
232 
233  connect(mAgenda, TQ_SIGNAL(mousePosSignal(const TQPoint &)), this, TQ_SLOT(mousePosChanged(const TQPoint &)));
234  connect(mAgenda, TQ_SIGNAL(enterAgenda()), this, TQ_SLOT(showMousePos()));
235  connect(mAgenda, TQ_SIGNAL(leaveAgenda()), this, TQ_SLOT(hideMousePos()));
236  connect(mAgenda, TQ_SIGNAL(gridSpacingYChanged( double ) ), this, TQ_SLOT( setCellHeight( double ) ) );
237 }
238 
239 
241 void TimeLabels::paintEvent(TQPaintEvent*)
242 {
243 // kdDebug(5850) << "paintevent..." << endl;
244  // this is another hack!
245 // TQPainter painter(this);
246  //TQString c
247  repaintContents(contentsX(), contentsY(), visibleWidth(), visibleHeight());
248 }
249 
250 #include "timelabels.moc"