kspread Library API Documentation

kspread_plugininsertcalendar.cc

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Raphael Langerhorst                             *
00003  *   raphael-langerhorst@gmx.at                                            *
00004  *                                                                         *
00005  *   Permission is hereby granted, free of charge, to any person obtaining *
00006  *   a copy of this software and associated documentation files (the       *
00007  *   "Software"), to deal in the Software without restriction, including   *
00008  *   without limitation the rights to use, copy, modify, merge, publish,   *
00009  *   distribute, sublicense, and/or sell copies of the Software, and to    *
00010  *   permit persons to whom the Software is furnished to do so, subject to *
00011  *   the following conditions:                                             *
00012  *                                                                         *
00013  *   The above copyright notice and this permission notice shall be        *
00014  *   included in all copies or substantial portions of the Software.       *
00015  *                                                                         *
00016  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
00017  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
00018  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
00019  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
00020  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
00021  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
00022  *   OTHER DEALINGS IN THE SOFTWARE.                                       *
00023  ***************************************************************************/
00024 
00025 #include "kspread_plugininsertcalendar.h"
00026 
00027 #include "kspread_insertcalendardialog.h"
00028 
00029 #include "../../kspread_view.h"
00030 #include "../../kspread_doc.h"
00031 #include "../../kspread_selection.h"
00032 #include "../../kspread_sheet.h"
00033 
00034 #include <kcalendarsystem.h>
00035 #include <kcalendarsystemfactory.h>
00036 #include <kaboutdata.h>
00037 #include <kgenericfactory.h>
00038 #include <klocale.h>
00039 #include <kofficeversion.h>
00040 #include <kmessagebox.h>
00041 
00042 #include <qpoint.h>
00043 #include <qrect.h>
00044 
00045 namespace KSpread
00046 {
00047 
00048 // make the plugin available
00049 typedef KGenericFactory<PluginInsertCalendar> InsertCalendarFactory;
00050 K_EXPORT_COMPONENT_FACTORY( libkspreadinsertcalendar,  InsertCalendarFactory("kspreadinsertcalendar"))
00051 
00052 
00053 PluginInsertCalendar::PluginInsertCalendar( QObject *parent, const char *name, const QStringList& args )
00054 : Plugin(parent,name)
00055 {
00056     this->m_kspreadView = NULL;
00057     if (parent)
00058     {
00059       if (parent->inherits("KSpreadView"))
00060       {
00061         this->m_kspreadView = (KSpreadView*)parent;
00062       }
00063       else
00064       {
00065         kdWarning() << "Parent does not inherit KSpreadView!!!" << endl;
00066       }
00067     }
00068     else
00069     {
00070       kdWarning() << "Plugin created without a parent!!!" << endl;
00071     }
00072 
00073 
00074     this->m_dialog = NULL;
00075 
00076     (void)new KAction( i18n("Insert Calendar..."), KShortcut::null(),
00077                    this, SLOT( slotShowDialog() ), actionCollection(), "kspreadinsertcalendar");
00078 }
00079 
00080 PluginInsertCalendar::~PluginInsertCalendar()
00081 {
00082 }
00083 
00084 KAboutData* PluginInsertCalendar::createAboutData()
00085 {
00086   KAboutData * aboutData = new KAboutData(
00087     "kspreadinsertcalendar",  //app name
00088     I18N_NOOP("Insert Calendar"),  //program name
00089     KOFFICE_VERSION_STRING,  //version
00090     I18N_NOOP("KSpread Insert Calendar Plugin"),  //short description
00091     KAboutData::License_BSD,  //license type
00092     I18N_NOOP("(c) 2005, The KSpread Team"),  //copyright
00093     0,//I18N_NOOP("The Insert Calendar plugin can be used in spreadsheets"),  //text
00094     "http://www.koffice.org/kspread/");
00095   aboutData->addAuthor("Raphael Langerhorst", 0, "Raphael.Langerhorst@kdemail.net");
00096 
00097   return aboutData;
00098 }
00099 
00100 void PluginInsertCalendar::slotShowDialog()
00101 {
00102   kdDebug() << "slotShowDialog..." << endl;
00103 
00104   if (this->m_dialog == NULL)
00105   {
00106     this->m_dialog = new InsertCalendarDialog();
00107 
00108     Q_ASSERT(m_dialog);
00109 
00110     connect(m_dialog,SIGNAL(insertCalendar(const QDate&, const QDate&)),
00111             this,SLOT(slotInsertCalendar(const QDate&, const QDate&)));
00112   }
00113 
00114   //@todo if anyone knows a better way to get a background window to foreground, please change this...
00115   m_dialog->hide();
00116   m_dialog->show();
00117 
00118 }
00119 
00120 void PluginInsertCalendar::slotInsertCalendar(const QDate &start, const QDate &end)
00121 {
00122   //@todo implement
00123   kdDebug() << "slotInsert... still to be implemented" << endl;
00124 
00125   KSpreadDoc* document = m_kspreadView->doc();
00126 
00127   if (!document)
00128   {
00129     KMessageBox::error(NULL,i18n("Can't insert calendar because no document is set!"),i18n("Error"));
00130     return;
00131   }
00132 
00133   if (end < start)
00134   {
00135     KMessageBox::error(NULL,i18n("End date is before start date! Please make sure that end date comes after start date."),i18n("Error"));
00136     return;
00137   }
00138 
00139   if (start.daysTo(end) > 3652)
00140   {
00141     KMessageBox::error(NULL,i18n("Calendars shouldn't be longer than 10 years. If you really need such long periods you need to split them up."),i18n("Error"));
00142     return;
00143   }
00144 
00145   if (start == end)
00146   {
00147     if (KMessageBox::No == KMessageBox::warningYesNo(NULL,i18n("Start and end dates are equal! Only one day will be inserted, do you want to continue?"),i18n("Warning")))
00148       return;
00149   }
00150 
00151   if (start.daysTo(end)> 366)
00152   {
00153      if (KMessageBox::No == KMessageBox::warningYesNo(NULL,i18n("Creating a calendar for a longer period than a year can take up a lot of space, do you want to continue?"),i18n("Warning")))
00154       return;
00155   }
00156 
00157   KSpreadSelection* selection_info = m_kspreadView->selectionInfo();
00158 
00159   Q_ASSERT(selection_info);
00160 
00161   QPoint selection = selection_info->selection().topLeft();
00162   
00163   KSpreadSheet* sheet = m_kspreadView->activeSheet();
00164   
00165   Q_ASSERT(sheet);
00166   
00167   if (!sheet)
00168     return;
00169 
00170   //now let's check if the area is really empty...
00171   //we use two columns per day and one column for the week number
00172   int sizeX = 15;
00173   //we use two rows per week, some additional space between months...
00174   //so that should be ok, but can be improved of course
00175   //@todo improve calendar size prediction!
00176   int sizeY = 4 + (int)(0.5*(float)(start.daysTo(end)));
00177   
00178   if (!sheet->areaIsEmpty(QRect(selection,QSize(sizeX,sizeY))))
00179   {
00180     if (KMessageBox::No == KMessageBox::warningYesNo(NULL,i18n("The area where the calendar is inserted is NOT empty, are you sure you want to continue, overwriting existing data? If you choose No the area that would be required for the desired calendar will be selected so you can see what data would be overwritten."),i18n("Warning")))
00181     {
00182       //select the area so the user knows what's in the way
00183       selection_info->setSelection(selection,QPoint(selection.x()+sizeX,selection.y()+sizeY),sheet);
00184       return;
00185     }
00186   }
00187 
00188   KCalendarSystem* cs = KCalendarSystemFactory::create();
00189 
00190   Q_ASSERT(cs);
00191 
00192   document->emitBeginOperation();
00193 
00194   int row = selection.y();
00195   int col = selection.x();
00196   int colstart = col; //this is where we get back after each week
00197   sheet->setText(row,colstart,i18n("Calendar from %1 to %2").arg(start.toString()).arg(end.toString()));
00198 
00199   QDate current(start);
00200 //   QDate previous(current);
00201   bool yearheader = true;
00202   bool monthheader = true;
00203   bool weekheader = true;
00204 
00205   //this loop creates the actual calendar
00206   //@todo formatting of cells - each day occupies QRect(row,col,row,col+1)
00207   while (current <= end)
00208   {
00209 
00210     //let's see if any header is required
00211     if (cs->dayOfWeek(current)==1)
00212     {
00213       col=colstart;
00214       row++;
00215       weekheader=true;
00216     }
00217     if (cs->day(current)==1)
00218     {
00219       row+=2;
00220       col=colstart + (cs->dayOfWeek(current)-1)*2;
00221       monthheader=true;
00222       weekheader=true;
00223       if (cs->month(current)==1)
00224       {
00225         row++;
00226         yearheader=true;
00227       }
00228     }
00229 
00230     if (yearheader)
00231     {
00232       kdDebug() << "inserting year " + QString::number(current.year()) << endl;      
00233       sheet->setText(row,colstart+6,cs->yearString(current,false));
00234       
00235       row+=2;
00236       yearheader=false;
00237     }
00238     if (monthheader)
00239     {
00240       kdDebug() << "inserting month " + QString::number(current.month()) << endl;      
00241       sheet->setText(row,colstart+6,cs->monthName(current,false));
00242       row+=2;
00243       //we always have the week number in the first column
00244       sheet->setText(row,colstart,i18n("week"));
00245       for (int i=1; i<8; i++)
00246       {
00247         sheet->setText(row,colstart+(i-1)*2+1,cs->weekDayName(i));
00248       }
00249       row++;
00250       monthheader=false;
00251     }
00252     if (weekheader)
00253     {
00254       sheet->setText(row,colstart,QString::number(cs->weekNumber(current)));
00255       col++;
00256       weekheader=false;
00257 
00258       //if we are at the beginning of the month we might need an offset
00259       if (cs->day(current)==1)
00260       {
00261         col=colstart + (cs->dayOfWeek(current)-1)*2 + 1;
00262       }
00263     }
00264 
00265     sheet->setText(row,col,QString::number(cs->day(current)));    
00266     //go to the next date
00267     //@todo isn't there a better way, like current++ or something??
00268     QDate next = current.addDays(1);
00269     current.setYMD(next.year(),next.month(),next.day());
00270     col+=2;
00271 
00272   }
00273 
00274   document->emitEndOperation();
00275 
00276   kdDebug() << "inserting calendar completed" << endl;
00277 }
00278 
00279 }
00280 
00281 #include "kspread_plugininsertcalendar.moc"
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:13 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003