summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoDetailsPane.cpp
blob: efddc96a62762e881bc495d8660582f7cac0d5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* This file is part of the KDE project
   Copyright (C) 2005 Peter Simonsson <psn@linux.se>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
#include "KoDetailsPane.h"

#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqfile.h>
#include <tqimage.h>
#include <tqheader.h>
#include <tqrect.h>
#include <tqbrush.h>
#include <tqpainter.h>
#include <tqsimplerichtext.h>
#include <tqevent.h>
#include <tqsplitter.h>

#include <kinstance.h>
#include <klocale.h>
#include <klistview.h>
#include <kpushbutton.h>
#include <kconfig.h>
#include <kurl.h>
#include <kfileitem.h>
#include <kio/previewjob.h>
#include <kdebug.h>
#include <ktextbrowser.h>
#include <kapplication.h>

#include "KoTemplates.h"

class KoFileListItem : public KListViewItem
{
  public:
    KoFileListItem(KListView* listView, TQListViewItem* after, const TQString& filename,
                   const TQString& fullPath, KFileItem* fileItem)
      : KListViewItem(listView, after, filename, fullPath), m_fileItem(fileItem)
    {
    }

    ~KoFileListItem()
    {
      delete m_fileItem;
    }

    KFileItem* fileItem() const
    {
      return m_fileItem;
    }

  private:
    KFileItem* m_fileItem;
};

class KoTemplatesPanePrivate
{
  public:
    KoTemplatesPanePrivate()
      : m_instance(0), m_selected(false)
    {
    }

    KInstance* m_instance;
    bool m_selected;
    TQString m_alwaysUseTemplate;
};


KoTemplatesPane::KoTemplatesPane(TQWidget* tqparent, KInstance* instance,
                                 KoTemplateGroup *group, KoTemplate* /*defaultTemplate*/)
  : KoDetailsPaneBase(tqparent, "TemplatesPane")
{
  d = new KoTemplatesPanePrivate;
  d->m_instance = instance;
  m_previewLabel->installEventFilter(this);
  m_documentList->installEventFilter(this);
  setFocusProxy(m_documentList);
#if KDE_IS_VERSION(3,4,0)
  m_documentList->setShadeSortColumn(false);
#endif

  KGuiItem openGItem(i18n("Use This Template"));
  m_openButton->setGuiItem(openGItem);
  m_documentList->header()->hide();
  KConfigGroup cfgGrp(d->m_instance->config(), "TemplateChooserDialog");
  TQString fullTemplateName = cfgGrp.readPathEntry("FullTemplateName");
  d->m_alwaysUseTemplate = cfgGrp.readPathEntry("AlwaysUseTemplate");
  connect(m_alwaysUseCheckBox, TQT_SIGNAL(clicked()), this, TQT_SLOT(alwaysUseClicked()));
  changePalette();

  if(kapp) {
    connect(kapp, TQT_SIGNAL(kdisplayPaletteChanged()), this, TQT_SLOT(changePalette()));
  }

  KListViewItem* selectItem = 0;

  for (KoTemplate* t = group->first(); t != 0L; t = group->next()) {
    KListViewItem* item = new KListViewItem(m_documentList, t->name(), t->description(), t->file());
    TQImage icon = t->loadPicture(instance).convertToImage();
    icon = icon.smoothScale(64, 64, TQ_ScaleMin);
    icon.setAlphaBuffer(true);
    icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
    item->setPixmap(0, TQPixmap(icon));
    item->setPixmap(2, t->loadPicture(instance));

    if(d->m_alwaysUseTemplate == t->file()) {
      selectItem = item;
    } else if(!selectItem && (t->file() == fullTemplateName)) {
      selectItem = item;
    }
  }

  connect(m_documentList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
          this, TQT_SLOT(selectionChanged(TQListViewItem*)));
  connect(m_documentList, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)),
          this, TQT_SLOT(openTemplate(TQListViewItem*)));
  connect(m_documentList, TQT_SIGNAL(returnPressed(TQListViewItem*)),
          this, TQT_SLOT(openTemplate(TQListViewItem*)));
  connect(m_openButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(openTemplate()));

  if(selectItem) {
    m_documentList->setSelected(selectItem, true);
    d->m_selected = true;
  } else {
    m_documentList->setSelected(m_documentList->firstChild(), true);
  }
}

KoTemplatesPane::~KoTemplatesPane()
{
  delete d;
}

void KoTemplatesPane::selectionChanged(TQListViewItem* item)
{
  if(item) {
    m_openButton->setEnabled(true);
    m_alwaysUseCheckBox->setEnabled(true);
    m_titleLabel->setText(item->text(0));
    m_previewLabel->setPixmap(*(item->pixmap(2)));
    m_detailsLabel->setText(item->text(1));
    m_alwaysUseCheckBox->setChecked(item->text(2) == d->m_alwaysUseTemplate);
  } else {
    m_openButton->setEnabled(false);
    m_alwaysUseCheckBox->setEnabled(false);
    m_alwaysUseCheckBox->setChecked(false);
    m_titleLabel->setText(TQString());
    m_previewLabel->setPixmap(TQPixmap());
  }
}

void KoTemplatesPane::openTemplate()
{
  TQListViewItem* item = m_documentList->selectedItem();
  openTemplate(item);
}

void KoTemplatesPane::openTemplate(TQListViewItem* item)
{
  if(item) {
    KConfigGroup cfgGrp(d->m_instance->config(), "TemplateChooserDialog");
    cfgGrp.writePathEntry("FullTemplateName", item->text(2));
    cfgGrp.writeEntry("LastReturnType", "Template");
    cfgGrp.writeEntry("AlwaysUseTemplate", d->m_alwaysUseTemplate);
    emit openTemplate(item->text(2));
  }
}

void KoTemplatesPane::changePalette()
{
  TQPalette p = kapp ? kapp->palette() : palette();
  p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Normal, TQColorGroup::Background));
  p.setColor(TQColorGroup::Text, p.color(TQPalette::Normal, TQColorGroup::Foreground));
  m_detailsLabel->setPalette(p);
}

bool KoTemplatesPane::isSelected()
{
  return d->m_selected;
}

void KoTemplatesPane::alwaysUseClicked()
{
  TQListViewItem* item = m_documentList->selectedItem();

  if(!m_alwaysUseCheckBox->isChecked()) {
    KConfigGroup cfgGrp(d->m_instance->config(), "TemplateChooserDialog");
    cfgGrp.writeEntry("AlwaysUseTemplate", TQString());
    d->m_alwaysUseTemplate = TQString();
  } else {
    d->m_alwaysUseTemplate = item->text(2);
  }

  emit alwaysUseChanged(this, d->m_alwaysUseTemplate);
}

void KoTemplatesPane::changeAlwaysUseTemplate(KoTemplatesPane* sender, const TQString& alwaysUse)
{
  if(this == sender)
    return;

  TQListViewItem* item = m_documentList->selectedItem();

  // If the old always use template is selected uncheck the checkbox
  if(item && (item->text(2) == d->m_alwaysUseTemplate)) {
    m_alwaysUseCheckBox->setChecked(false);
  }

  d->m_alwaysUseTemplate = alwaysUse;
}

bool KoTemplatesPane::eventFilter(TQObject* watched, TQEvent* e)
{
  if(TQT_BASE_OBJECT(watched) == m_previewLabel) {
    if(e->type() == TQEvent::MouseButtonDblClick) {
      openTemplate();
    }
  }

  if(TQT_BASE_OBJECT(watched) == m_documentList) {
    if((e->type() == TQEvent::Resize) && isShown()) {
      emit splitterResized(this, m_splitter->sizes());
    }
  }

  return false;
}

void KoTemplatesPane::resizeSplitter(KoDetailsPaneBase* sender, const TQValueList<int>& sizes)
{
  if(sender == this)
    return;

  m_splitter->setSizes(sizes);
}


class KoRecentDocumentsPanePrivate
{
  public:
    KoRecentDocumentsPanePrivate()
      : m_previewJob(0), m_instance(0)
    {
    }

    ~KoRecentDocumentsPanePrivate()
    {
      if(m_previewJob)
        m_previewJob->kill();
    }

    KIO::PreviewJob* m_previewJob;
    KInstance* m_instance;
};

KoRecentDocumentsPane::KoRecentDocumentsPane(TQWidget* tqparent, KInstance* instance)
  : KoDetailsPaneBase(tqparent, "RecentDocsPane")
{
  d = new KoRecentDocumentsPanePrivate;
  d->m_instance = instance;
  m_previewLabel->installEventFilter(this);
  m_documentList->installEventFilter(this);
  setFocusProxy(m_documentList);
  KGuiItem openGItem(i18n("Open This Document"), "fileopen");
  m_openButton->setGuiItem(openGItem);
  m_alwaysUseCheckBox->hide();
  m_documentList->header()->hide();
  m_documentList->setSorting(-1); // Disable sorting
  changePalette();

  if(kapp) {
    connect(kapp, TQT_SIGNAL(kdisplayPaletteChanged()), this, TQT_SLOT(changePalette()));
  }

  TQString oldGroup = instance->config()->group();
  instance->config()->setGroup("RecentFiles");

  int i = 0;
  TQString value;
  KFileItemList fileList;
  fileList.setAutoDelete(false);

  do {
    TQString key = TQString("File%1").tqarg(i);
    value = instance->config()->readPathEntry(key);

    if(!value.isEmpty()) {
      TQString path = value;
      TQString name;

      // Support for kdelibs-3.5's new RecentFiles format: name[url]
      if(path.endsWith("]")) {
        int pos = path.find("[");
        name = path.mid(0, pos - 1);
        path = path.mid(pos + 1, path.length() - pos - 2);
      }

      KURL url(path);

      if(name.isEmpty())
        name = url.filename();

      if(!url.isLocalFile() || TQFile::exists(url.path())) {
        KFileItem* fileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
        fileList.append(fileItem);
        KoFileListItem* item = new KoFileListItem(m_documentList,
            m_documentList->lastItem(), name, url.url(), fileItem);
        //center all icons in 64x64 area
        TQImage icon = fileItem->pixmap(64).convertToImage();
        icon.setAlphaBuffer(true);
        icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
        item->setPixmap(0, TQPixmap(icon));
        item->setPixmap(2, fileItem->pixmap(128));
      }
    }

    i++;
  } while ( !value.isEmpty() || i<=10 );

  instance->config()->setGroup( oldGroup );

  connect(m_documentList, TQT_SIGNAL(selectionChanged(TQListViewItem*)),
          this, TQT_SLOT(selectionChanged(TQListViewItem*)));
  connect(m_documentList, TQT_SIGNAL(clicked(TQListViewItem*)),
          this, TQT_SLOT(selectionChanged(TQListViewItem*)));
  connect(m_documentList, TQT_SIGNAL(doubleClicked(TQListViewItem*, const TQPoint&, int)),
          this, TQT_SLOT(openFile(TQListViewItem*)));
  connect(m_documentList, TQT_SIGNAL(returnPressed(TQListViewItem*)),
          this, TQT_SLOT(openFile(TQListViewItem*)));
  connect(m_openButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(openFile()));

  m_documentList->setSelected(m_documentList->firstChild(), true);

  d->m_previewJob = KIO::filePreview(fileList, 200, 200);

  connect(d->m_previewJob, TQT_SIGNAL(result(KIO::Job*)), this, TQT_SLOT(previewResult(KIO::Job*)));
  connect(d->m_previewJob, TQT_SIGNAL(gotPreview(const KFileItem*, const TQPixmap&)),
          this, TQT_SLOT(updatePreview(const KFileItem*, const TQPixmap&)));
}

KoRecentDocumentsPane::~KoRecentDocumentsPane()
{
  delete d;
}

void KoRecentDocumentsPane::selectionChanged(TQListViewItem* item)
{
  if(item) {
    m_openButton->setEnabled(true);
    m_titleLabel->setText(item->text(0));
    m_previewLabel->setPixmap(*(item->pixmap(2)));

    if(static_cast<KoFileListItem*>(item)->fileItem()) {
      KFileItem* fileItem = static_cast<KoFileListItem*>(item)->fileItem();
      TQString details = "<center><table border=\"0\">";
      details += i18n("File modification date and time. %1 is date time", "<tr><td><b>Modified:</b></td><td>%1</td></tr>")
          .tqarg(fileItem->timeString(KIO::UDS_MODIFICATION_TIME));
      details += i18n("File access date and time. %1 is date time", "<tr><td><b>Accessed:</b></td><td>%1</td></tr>")
          .tqarg(fileItem->timeString(KIO::UDS_ACCESS_TIME));
      details += "</table></center>";
      m_detailsLabel->setText(details);
    } else {
      m_detailsLabel->setText(TQString());
    }
  } else {
    m_openButton->setEnabled(false);
    m_titleLabel->setText(TQString());
    m_previewLabel->setPixmap(TQPixmap());
    m_detailsLabel->setText(TQString());
  }
}

void KoRecentDocumentsPane::openFile()
{
  TQListViewItem* item = m_documentList->selectedItem();
  openFile(item);
}

void KoRecentDocumentsPane::openFile(TQListViewItem* item)
{
  KConfigGroup cfgGrp(d->m_instance->config(), "TemplateChooserDialog");
  cfgGrp.writeEntry("LastReturnType", "File");

  if(item)
    emit openFile(item->text(1));
}

void KoRecentDocumentsPane::previewResult(KIO::Job* job)
{
  if(d->m_previewJob == job)
    d->m_previewJob = 0;
}

void KoRecentDocumentsPane::updatePreview(const KFileItem* fileItem, const TQPixmap& preview)
{
  if(preview.isNull()) {
    return;
  }

  TQListViewItemIterator it(m_documentList);

  while(it.current()) {
    if(it.current()->text(1) == fileItem->url().url()) {
      it.current()->setPixmap(2, preview);
      TQImage icon = preview.convertToImage();
      icon = icon.smoothScale(64, 64, TQ_ScaleMin);
      icon.setAlphaBuffer(true);
      icon = icon.copy((icon.width() - 64) / 2, (icon.height() - 64) / 2, 64, 64);
      it.current()->setPixmap(0, TQPixmap(icon));

      if(it.current()->isSelected()) {
        m_previewLabel->setPixmap(preview);
      }

      break;
    }

    it++;
  }
}

void KoRecentDocumentsPane::changePalette()
{
  TQPalette p = kapp ? kapp->palette() : palette();
  p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Normal, TQColorGroup::Background));
  p.setColor(TQColorGroup::Text, p.color(TQPalette::Normal, TQColorGroup::Foreground));
  m_detailsLabel->setPalette(p);
}

bool KoRecentDocumentsPane::eventFilter(TQObject* watched, TQEvent* e)
{
  if(TQT_BASE_OBJECT(watched) == m_previewLabel) {
    if(e->type() == TQEvent::MouseButtonDblClick) {
      openFile();
    }
  }

  if(TQT_BASE_OBJECT(watched) == m_documentList) {
    if((e->type() == TQEvent::Resize) && isShown()) {
      emit splitterResized(this, m_splitter->sizes());
    }
  }

  return false;
}

void KoRecentDocumentsPane::resizeSplitter(KoDetailsPaneBase* sender, const TQValueList<int>& sizes)
{
  if(sender == this)
    return;

  m_splitter->setSizes(sizes);
}

#include "KoDetailsPane.moc"