kspread

CalendarToolWidget.cpp

Go to the documentation of this file.
00001  /***************************************************************************
00002  *   Copyright (C) 2005 by Raphael Langerhorst <raphael-langerhorst@gmx.at>*
00003  *                                                                         *
00004  *   Permission is hereby granted, free of charge, to any person obtaining *
00005  *   a copy of this software and associated documentation files (the       *
00006  *   "Software"), to deal in the Software without restriction, including   *
00007  *   without limitation the rights to use, copy, modify, merge, publish,   *
00008  *   distribute, sublicense, and/or sell copies of the Software, and to    *
00009  *   permit persons to whom the Software is furnished to do so, subject to *
00010  *   the following conditions:                                             *
00011  *                                                                         *
00012  *   The above copyright notice and this permission notice shall be        *
00013  *   included in all copies or substantial portions of the Software.       *
00014  *                                                                         *
00015  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
00016  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
00017  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
00018  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
00019  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
00020  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
00021  *   OTHER DEALINGS IN THE SOFTWARE.                                       *
00022  ***************************************************************************/
00023 
00024 #include <CalendarToolWidget.h>
00025 
00026 #include <kdatepicker.h>
00027 #include <kdatewidget.h>
00028 #include <kdebug.h>
00029 
00030 #include <QPushButton>
00031 
00032 namespace KSpread
00033 {
00034 
00035 CalendarToolWidget::CalendarToolWidget(QWidget* parent)
00036   : QDialog(parent)
00037 {
00038   setupUi(this);
00039   this->m_datePicker = 0;
00040 
00041   //we start with a default calendar for the current month;
00042 
00043   QDate first_day_in_month = QDate::currentDate();
00044   first_day_in_month.setYMD(first_day_in_month.year(),first_day_in_month.month(),1);
00045 
00046   QDate last_day_in_month(first_day_in_month.year(),first_day_in_month.month(),first_day_in_month.daysInMonth());
00047 
00048   this->m_startDateWidget->setDate(first_day_in_month);
00049   this->m_endDateWidget->setDate(last_day_in_month);
00050 
00051   this->m_selectStartDateButton->setIcon(KIcon("office-calendar"));
00052   this->m_selectEndDateButton->setIcon(KIcon("office-calendar"));
00053 
00054   connect(this->m_selectStartDateButton,SIGNAL(clicked()),this,SLOT(showStartDatePicker()));
00055   connect(this->m_selectEndDateButton,SIGNAL(clicked()),this,SLOT(showEndDatePicker()));
00056 
00057   connect(this->m_insertButton, SIGNAL(clicked()), this, SLOT(emitInsertCalendar()));
00058 }
00059 
00060 CalendarToolWidget::~CalendarToolWidget()
00061 {
00062 }
00063 
00064 bool CalendarToolWidget::buildDatePickerFrame()
00065 {
00066   if (m_datePicker)
00067   {
00068     delete m_datePicker; //destroyed signal is connected to datePickerDeleted()
00069   }
00070 
00071   m_datePicker = new KDatePicker();
00072 
00073   Q_ASSERT(m_datePicker);
00074 
00075   if (!m_datePicker)
00076     return false;
00077 
00078   connect(m_datePicker,SIGNAL(destroyed()),this,SLOT(datePickerDeleted()));
00079 
00080   const QPoint position = mapToGlobal(pos());
00081   m_datePicker->move(position.x() + this->width(), position.y());
00082   m_datePicker->show();
00083 
00084   return true;
00085 }
00086 
00087 void CalendarToolWidget::datePickerDeleted()
00088 {
00089   kDebug() <<"date picker deleted";
00090   m_datePicker = 0;
00091 }
00092 
00093 void CalendarToolWidget::emitInsertCalendar()
00094 {
00095   if (m_datePicker)
00096     m_datePicker->deleteLater();
00097   emit insertCalendar(startDate(),endDate());
00098 }
00099 
00100 void CalendarToolWidget::showStartDatePicker()
00101 {
00102   if (buildDatePickerFrame())
00103   {
00104     connect(m_datePicker,SIGNAL(dateSelected(QDate)),this,SLOT(setStartDate(QDate)));
00105     connect(m_datePicker,SIGNAL(dateEntered(QDate)),this,SLOT(setStartDate(QDate)));
00106     m_datePicker->setDate(startDate());
00107   }
00108 }
00109 
00110 void CalendarToolWidget::showEndDatePicker()
00111 {
00112   if (buildDatePickerFrame())
00113   {
00114     connect(m_datePicker,SIGNAL(dateSelected(QDate)),this,SLOT(setEndDate(QDate)));
00115     connect(m_datePicker,SIGNAL(dateEntered(QDate)),this,SLOT(setEndDate(QDate)));
00116     m_datePicker->setDate(endDate());
00117   }
00118 }
00119 
00120 void CalendarToolWidget::setStartDate(const QDate& date)
00121 {
00122   this->m_startDateWidget->setDate(date);
00123 }
00124 
00125 void CalendarToolWidget::setEndDate(const QDate& date)
00126 {
00127   this->m_endDateWidget->setDate(date);
00128 }
00129 
00130 QDate CalendarToolWidget::startDate() const
00131 {
00132   return this->m_startDateWidget->date();
00133 }
00134 
00135 QDate CalendarToolWidget::endDate() const
00136 {
00137   return this->m_endDateWidget->date();
00138 }
00139 
00140 }
00141 
00142 #include "CalendarToolWidget.moc"