summaryrefslogtreecommitdiffstats
path: root/src/libgui/editor.cpp
blob: c64d98f2173b480e4eeba391ebe1152452178942 (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
/***************************************************************************
 *   Copyright (C) 2005 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "editor.h"

#include <klocale.h>

#include "common/gui/purl_gui.h"
#include "common/gui/misc_gui.h"

Editor::Editor(const QString &title, const QString &tag, QWidget *parent, const char *name)
  : QWidget(parent, name), _title(title), _tag(tag)
{}

Editor::Editor(QWidget *parent, const char *name)
  : QWidget(parent, name)
{}

QSizePolicy Editor::sizePolicy() const
{
  return QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

void Editor::setModified(bool m)
{
  setModifiedInternal(m);
  if (m) emit modified();
}

void Editor::setReadOnly(bool ro)
{
  setReadOnlyInternal(ro);
  emit guiChanged();
}

bool Editor::save()
{
  if ( url().isEmpty() ) return saveAs();
  if ( !save(url()) ) return false;
  setModified(false);
  emit guiChanged();
  return true;
}

bool Editor::saveAs()
{
  QString filter = PURL::filter(fileType());
  PURL::Url purl = PURL::getSaveUrl(":save_file_as", filter, this, i18n("Save File"), PURL::AskOverwrite);
  if ( purl.isEmpty() ) return false;
  if ( !save(purl) ) return false;
  setModified(false);
  emit guiChanged();
  return true;
}

bool Editor::slotLoad()
{
  bool readOnly = isReadOnly();
  if ( !open(url()) ) return false;
  setModified(false);
  setReadOnly(readOnly);
  statusChanged();
  emit guiChanged();
  return true;
}

QString Editor::filename() const
{
  return (url().isEmpty() ? "<" + _title + ">" : "\"" + url().filepath() + "\"");
}

bool Editor::checkSaved()
{
  if ( !isModified() ) return true;
  MessageBox::Result res = MessageBox::questionYesNoCancel(i18n("File %1 not saved.").arg(filename()),
                                                           KStdGuiItem::save(), KStdGuiItem::discard());
  if ( res==MessageBox::Cancel ) return false;
  if ( res==MessageBox::Yes ) save();
  return true;
}

bool Editor::reload()
{
  if ( !checkSaved() ) return false;
  return slotLoad();
}