[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: Date and time widgets
From:       Antonio Larrosa =?iso-8859-1?q?Jim=E9nez?= <larrosa () kde ! org>
Date:       2002-12-28 22:48:07
[Download RAW message or body]

El Jueves, 26 de Diciembre de 2002 15:30, Hans Petter Bieker escribió:
> On Wed, 25 Dec 2002, Cornelius Schumacher wrote:
> > Are you aware of the date and time selection widgets in
> > kdepim/libkdepim/kdateedit.{h,cpp} and
> > kdepim/korganizer/ktimeedit.{h,cpp}?
>
> I am now, but I wasn't. Perhaps we should try to merge kdatewidget with
> kdateedit and ktimewidget with ktimeedit. I guess the reason why we have
> a few such widgets laying around is the lack of features in the current
> widgets.
>

I added some weeks ago some methods to KDateTable but didn't commit them 
yet (but I'm using them in a new app I'm developing).

I didn't had any conflict (surprisingly :) ) when updating cvs after all 
those changes you've been doing in those classes, but anyway, I'd like to 
commit my changes as soon as possible.

They add support to specify the color in which to paint a given day, and 
also, if you want to mark days with a filled square or circle, in order to 
keep those days "highlighted" in an special way.

The change to KDatePicker is just an accesor to get the KDateTable object 
that KDatePicker uses to be able to call the methods I added there.

Also, I added a signal aboutToShowContextMenu( KPopupMenu * menu, const 
QDate &date); so that you can make KDateTable open a menu when you right 
click on a date and add entries before it's shown, depending on the date 
the user clicked on.

Should I commit the attached patch ?

Greetings, and merry christmas,

--
Antonio Larrosa Jimenez
KDE developer - larrosa@kde.org
http://developer.kde.org/~larrosa/
Take care of self-appointed experts.
["datechanges.diff" (text/x-diff)]

Index: kdatepicker.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kdatepicker.h,v
retrieving revision 1.30
diff -u -p -r1.30 kdatepicker.h
--- kdatepicker.h	28 Sep 2002 15:16:22 -0000	1.30
+++ kdatepicker.h	28 Dec 2002 22:34:31 -0000
@@ -113,6 +113,8 @@ public:
    **/
   void setEnabled(bool);
 
+  KDateTable *dateTable() const { return table; };
+  
   /**
    * Sets the font size of the widgets elements.
    **/
Index: kdatetbl.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kdatetbl.cpp,v
retrieving revision 1.52
diff -u -p -r1.52 kdatetbl.cpp
--- kdatetbl.cpp	26 Sep 2002 18:42:04 -0000	1.52
+++ kdatetbl.cpp	28 Dec 2002 22:34:57 -0000
@@ -40,13 +40,43 @@
 #include <knotifyclient.h>
 #include "kdatepicker.h"
 #include "kdatetbl.h"
+#include "kpopupmenu.h"
 #include <qdatetime.h>
 #include <qstring.h>
 #include <qpen.h>
 #include <qpainter.h>
 #include <qdialog.h>
+#include <qdict.h>
 #include <assert.h>
 
+
+class KDateTable::KDateTablePrivate
+{
+public:
+   KDateTablePrivate()
+   {
+      popupMenuEnabled=false;
+      useCustomColors=false;
+   }
+
+   ~KDateTablePrivate()
+   {
+   }
+
+   bool popupMenuEnabled;
+   bool useCustomColors;
+
+   struct DatePaintingMode
+   {
+     QColor fgColor;
+     QColor bgColor;
+     BackgroundMode bgMode;
+   };
+   QDict <DatePaintingMode> customPaintingModes;
+   
+};
+
+
 KDateValidator::KDateValidator(QWidget* parent, const char* name)
     : QValidator(parent, name)
 {
@@ -81,6 +111,7 @@ KDateValidator::fixup( QString& ) const
 KDateTable::KDateTable(QWidget *parent, QDate date_, const char* name, WFlags f)
   : QGridView(parent, name, f)
 {
+  d = new KDateTablePrivate;
   setFontSize(10);
   if(!date_.isValid())
     {
@@ -96,6 +127,11 @@ KDateTable::KDateTable(QWidget *parent, 
   setDate(date_); // this initializes firstday, numdays, numDaysPrevMonth
 }
 
+KDateTable::~KDateTable()
+{
+  delete d;
+}
+
 void
 KDateTable::paintCell(QPainter *painter, int row, int col)
 {
@@ -143,6 +179,7 @@ KDateTable::paintCell(QPainter *painter,
       painter->lineTo(w-1, h-1);
       // ----- draw the weekday:
     } else {
+      bool paintRect=true;
       painter->setFont(font);
       pos=7*(row-1)+col;
       if ( firstWeekDay < 4 )
@@ -162,6 +199,26 @@ KDateTable::paintCell(QPainter *painter,
           painter->setPen(gray);
         } else { // paint a day of the current month
           text.setNum(pos-firstday+1);
+	  if ( d->useCustomColors )
+	  {
+	    const QString key=text+"-"+date.month()+"-"+date.year();
+	    KDateTablePrivate::DatePaintingMode *mode=d->customPaintingModes[key];
+	    if (mode)
+	    {
+	      QBrush oldbrush=painter->brush();
+	      painter->setBrush( mode->bgColor );
+	      switch(mode->bgMode)
+	      {
+	        case(CircleMode) : painter->drawEllipse(0,0,w,h);break;
+		case(RectangleMode) : painter->drawRect(0,0,w,h);break;
+		case(None) :
+		default: 
+	      }
+	      painter->setPen( mode->fgColor );
+	      painter->setBrush( oldbrush );
+	      paintRect=false;
+	    } else
+	      painter->setPen(KGlobalSettings::textColor());
+	  } else
           painter->setPen(KGlobalSettings::textColor());
         }
 
@@ -191,7 +248,7 @@ KDateTable::paintCell(QPainter *painter,
          painter->setPen(KGlobalSettings::textColor());
       }
 
-      painter->drawRect(0, 0, w, h);
+      if ( paintRect ) painter->drawRect(0, 0, w, h);
       painter->setPen(pen);
       painter->drawText(0, 0, w, h, AlignCenter, text, -1, &rect);
     }
@@ -340,14 +397,23 @@ KDateTable::contentsMousePressEvent(QMou
       setDate(date.addDays(pos - firstday - date.day() + dayoff % 7));
       return;
     }
+  
+  QDate clickedDate(QDate(date.year(), date.month(), pos - firstday + dayoff % 7));
   temp = firstday + date.day() - dayoff % 7 - 1;
 
-  setDate(QDate(date.year(), date.month(), pos - firstday + dayoff % 7));
-
+  setDate( clickedDate );
   updateCell(temp/7+1, temp%7); // Update the previously selected cell
   updateCell(row, col); // Update the selected cell
   // assert(QDate(date.year(), date.month(), pos-firstday+dayoff).isValid());
+  
   emit(tableClicked());
+  if (  e->button() == Qt::RightButton && d->popupMenuEnabled )
+  {
+	KPopupMenu *menu = new KPopupMenu();
+	menu->insertTitle( clickedDate.toString() );
+	emit aboutToShowContextMenu( menu, clickedDate );
+	menu->popup(e->globalPos());
+  }
 }
 
 bool
@@ -415,6 +481,29 @@ KDateTable::sizeHint() const
       kdDebug() << "KDateTable::sizeHint: obscure failure - " << endl;
       return QSize(-1, -1);
     }
+}
+
+void KDateTable::setPopupMenuEnabled( bool enable )
+{
+   d->popupMenuEnabled=enable;
+}
+
+bool KDateTable::popupMenuEnabled() const
+{
+   return d->popupMenuEnabled;
+}
+
+void KDateTable::setCustomDatePainting(const QDate &date, const QColor &fgColor, \
BackgroundMode bgMode, const QColor &bgColor) +{
+    KDateTablePrivate::DatePaintingMode *mode=new \
KDateTablePrivate::DatePaintingMode; +    mode->bgMode=bgMode;
+    mode->fgColor=fgColor;
+    mode->bgColor=bgColor;
+    const QString key=QString::number(date.day())+"-"+date.month()+"-"+date.year();
+		
+    d->customPaintingModes.replace( key, mode );
+    d->useCustomColors=true;
+    update();
 }
 
 KDateInternalWeekSelector::KDateInternalWeekSelector
Index: kdatetbl.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kdatetbl.h,v
retrieving revision 1.31
diff -u -p -r1.31 kdatetbl.h
--- kdatetbl.h	25 Jul 2002 13:26:37 -0000	1.31
+++ kdatetbl.h	28 Dec 2002 22:35:04 -0000
@@ -24,6 +24,9 @@
 #include <qgridview.h>
 #include <qlineedit.h>
 #include <qdatetime.h>
+#include <qcolor.h>
+
+class KPopupMenu;
 
 /** Week selection widget.
 * @internal
@@ -252,6 +255,12 @@ public:
     KDateTable(QWidget *parent=0,
 	       QDate date=QDate::currentDate(),
 	       const char* name=0, WFlags f=0);
+	       
+    /**
+     * The destructor.
+     */
+    ~KDateTable();
+    
     /**
      * Returns a recommended size for the widget.
      * To save some time, the size of the largest used cell content is
@@ -270,6 +279,11 @@ public:
     bool setDate(const QDate&);
     const QDate& getDate() const;
 
+    void setPopupMenuEnabled( bool enable );
+    bool popupMenuEnabled() const;
+
+    enum BackgroundMode { None=0, RectangleMode, CircleMode };
+    void setCustomDatePainting(const QDate &date, const QColor &fgColor, \
BackgroundMode bgMode=RectangleMode, const QColor &bgColor=QColor());  
 protected:
     /**
@@ -326,6 +340,7 @@ signals:
      * A date has been selected by clicking on the table.
      */
     void tableClicked();
+    void aboutToShowContextMenu( KPopupMenu * menu, const QDate &date);
 
 protected:
   virtual void virtual_hook( int id, void* data );



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic