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 "timelabels.h"
00026
00027 #include <qhbox.h>
00028 #include <qvbox.h>
00029 #include <qlabel.h>
00030 #include <qframe.h>
00031 #include <qlayout.h>
00032 #include <qfont.h>
00033 #include <qfontmetrics.h>
00034 #include <qpainter.h>
00035 #include <qstringlist.h>
00036 #include <qdatetime.h>
00037
00038 #include <kglobal.h>
00039
00040 #include "koglobals.h"
00041 #include "kocore.h"
00042 #include "koprefs.h"
00043 #include "koagenda.h"
00044
00045 TimeLabels::TimeLabels(int rows,QWidget *parent,const char *name,WFlags f) :
00046 QScrollView(parent,name,f)
00047 {
00048 mRows = rows;
00049 mMiniWidth = 0;
00050
00051 mCellHeight = KOPrefs::instance()->mHourSize*4;
00052
00053 enableClipper(true);
00054
00055 setHScrollBarMode(AlwaysOff);
00056 setVScrollBarMode(AlwaysOff);
00057
00058 resizeContents(50, int(mRows * mCellHeight) );
00059
00060 viewport()->setBackgroundMode( PaletteBackground );
00061
00062 mMousePos = new QFrame(this);
00063 mMousePos->setLineWidth(0);
00064 mMousePos->setMargin(0);
00065 mMousePos->setBackgroundColor(Qt::red);
00066 mMousePos->setFixedSize(width(), 1);
00067 addChild(mMousePos, 0, 0);
00068 }
00069
00070 void TimeLabels::mousePosChanged(const QPoint &pos)
00071 {
00072 moveChild(mMousePos, 0, pos.y());
00073 }
00074
00075 void TimeLabels::showMousePos()
00076 {
00077 mMousePos->show();
00078 }
00079
00080 void TimeLabels::hideMousePos()
00081 {
00082 mMousePos->hide();
00083 }
00084
00085 void TimeLabels::setCellHeight(double height)
00086 {
00087 mCellHeight = height;
00088 }
00089
00090
00091
00092
00093
00094 void TimeLabels::drawContents(QPainter *p,int cx, int cy, int cw, int ch)
00095 {
00096
00097
00098
00099
00100
00101 cx = contentsX() + frameWidth()*2;
00102 cw = contentsWidth() ;
00103
00104
00105 int cell = ((int)(cy/mCellHeight));
00106 double y = cell * mCellHeight;
00107 QFontMetrics fm = fontMetrics();
00108 QString hour;
00109 QString suffix = "am";
00110 int timeHeight = fm.ascent();
00111 QFont nFont = font();
00112 p->setFont( font() );
00113
00114 if (!KGlobal::locale()->use12Clock()) {
00115 suffix = "00";
00116 } else
00117 if (cell > 11) suffix = "pm";
00118
00119 if ( timeHeight > mCellHeight ) {
00120 timeHeight = int(mCellHeight-1);
00121 int pointS = nFont.pointSize();
00122 while ( pointS > 4 ) {
00123 nFont.setPointSize( pointS );
00124 fm = QFontMetrics( nFont );
00125 if ( fm.ascent() < mCellHeight )
00126 break;
00127 -- pointS;
00128 }
00129 fm = QFontMetrics( nFont );
00130 timeHeight = fm.ascent();
00131 }
00132
00133 QFont sFont = nFont;
00134 sFont.setPointSize( sFont.pointSize()/2 );
00135 QFontMetrics fmS( sFont );
00136 int startW = mMiniWidth - frameWidth()-2 ;
00137 int tw2 = fmS.width(suffix);
00138 int divTimeHeight = (timeHeight-1) /2 - 1;
00139
00140
00141 while (y < cy + ch+mCellHeight) {
00142
00143 p->drawLine( cx, int(y), cw+2, int(y) );
00144 hour.setNum(cell);
00145
00146 if (KGlobal::locale()->use12Clock()) {
00147 if (cell == 12) suffix = "pm";
00148 if (cell == 0) hour.setNum(12);
00149 if (cell > 12) hour.setNum(cell - 12);
00150 }
00151
00152
00153 int timeWidth = fm.width(hour);
00154 int offset = startW - timeWidth - tw2 -1 ;
00155 p->setFont( nFont );
00156 p->drawText( offset, int(y+timeHeight), hour);
00157 p->setFont( sFont );
00158 offset = startW - tw2;
00159 p->drawText( offset, int(y+timeHeight-divTimeHeight), suffix);
00160
00161
00162 y += mCellHeight;
00163 cell++;
00164 }
00165
00166 }
00167
00171 int TimeLabels::minimumWidth() const
00172 {
00173 return mMiniWidth;
00174 }
00175
00177 void TimeLabels::updateConfig()
00178 {
00179 setFont(KOPrefs::instance()->mTimeBarFont);
00180
00181 QString test = "20";
00182 if ( KGlobal::locale()->use12Clock() )
00183 test = "12";
00184 mMiniWidth = fontMetrics().width( test );
00185 if ( KGlobal::locale()->use12Clock() )
00186 test = "pm";
00187 else {
00188 test = "00";
00189 }
00190 QFont sFont = font();
00191 sFont.setPointSize( sFont.pointSize()/2 );
00192 QFontMetrics fmS( sFont );
00193 mMiniWidth += fmS.width( test ) + frameWidth()*2+4 ;
00194
00195 setFixedWidth( mMiniWidth );
00196
00197
00198 mCellHeight = KOPrefs::instance()->mHourSize*4;
00199
00200
00201
00202 if ( mCellHeight < 4*mAgenda->gridSpacingY() )
00203 mCellHeight = 4*mAgenda->gridSpacingY();
00204 resizeContents( mMiniWidth, int(mRows * mCellHeight+1) );
00205 }
00206
00208 void TimeLabels::positionChanged()
00209 {
00210 int adjustment = mAgenda->contentsY();
00211 setContentsPos(0, adjustment);
00212 }
00213
00214 void TimeLabels::positionChanged( int pos )
00215 {
00216 setContentsPos( 0, pos );
00217 }
00218
00220 void TimeLabels::setAgenda(KOAgenda* agenda)
00221 {
00222 mAgenda = agenda;
00223
00224 connect(mAgenda, SIGNAL(mousePosSignal(const QPoint &)), this, SLOT(mousePosChanged(const QPoint &)));
00225 connect(mAgenda, SIGNAL(enterAgenda()), this, SLOT(showMousePos()));
00226 connect(mAgenda, SIGNAL(leaveAgenda()), this, SLOT(hideMousePos()));
00227 connect(mAgenda, SIGNAL(gridSpacingYChanged( double ) ), this, SLOT( setCellHeight( double ) ) );
00228 }
00229
00230
00232 void TimeLabels::paintEvent(QPaintEvent*)
00233 {
00234
00235
00236
00237
00238 repaintContents(contentsX(), contentsY(), visibleWidth(), visibleHeight());
00239 }
00240
00241 #include "timelabels.moc"