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 "kspread_plugininsertcalendar.h"
00026
00027 #include "kspread_insertcalendardialog.h"
00028
00029 #include "../../kspread_view.h"
00030 #include "../../kspread_doc.h"
00031 #include "../../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
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& )
00054 : Plugin(parent,name)
00055 {
00056 this->m_kspreadView = NULL;
00057 if (parent)
00058 {
00059 if (parent->inherits("KSpread::View"))
00060 {
00061 this->m_kspreadView = (View*)parent;
00062 }
00063 else
00064 {
00065 kdWarning() << "Parent does not inherit View!!!" << 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",
00088 I18N_NOOP("Insert Calendar"),
00089 KOFFICE_VERSION_STRING,
00090 I18N_NOOP("KSpread Insert Calendar Plugin"),
00091 KAboutData::License_BSD,
00092 I18N_NOOP("(c) 2005, The KSpread Team"),
00093 0,
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
00115 m_dialog->hide();
00116 m_dialog->show();
00117
00118 }
00119
00120 void PluginInsertCalendar::slotInsertCalendar(const QDate &start, const QDate &end)
00121 {
00122
00123 kdDebug() << "slotInsert... still to be implemented" << endl;
00124
00125 Doc* 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 Selection* selection_info = m_kspreadView->selectionInfo();
00158
00159 Q_ASSERT(selection_info);
00160
00161 QPoint selection = selection_info->selection().topLeft();
00162
00163 Sheet* sheet = m_kspreadView->activeSheet();
00164
00165 Q_ASSERT(sheet);
00166
00167 if (!sheet)
00168 return;
00169
00170
00171
00172 int sizeX = 15;
00173
00174
00175
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
00183 selection_info->initialize(QRect(selection.x(),selection.y(),sizeX,sizeY));
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;
00197 sheet->setText(row,colstart,i18n("Calendar from %1 to %2").arg(start.toString()).arg(end.toString()));
00198
00199 QDate current(start);
00200
00201 bool yearheader = true;
00202 bool monthheader = true;
00203 bool weekheader = true;
00204
00205
00206
00207 while (current <= end)
00208 {
00209
00210
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
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
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
00267
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"