summaryrefslogtreecommitdiffstats
path: root/kcoloredit/kcoloreditdoc.cpp
blob: 954d64a3de3557462f9179f6db1cb62ef6706a06 (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
/***************************************************************************
                          kcoloreditdoc.cpp  -  description
                             -------------------
    begin                : Sat Jul  8 09:57:28 CEST 2000
    copyright            : (C) 2000 by Artur Rataj
    email                : art@zeus.polsl.gliwice.pl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 files for TQt
#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqwidget.h>
#include <tqclipboard.h>

// include files for KDE
#include <klocale.h>
#include <kmessagebox.h>

// application specific includes
#include "kcoloreditdoc.h"
#include "kcoloredit.h"
#include "kcoloreditview.h"
#include "resource.h"

KColorEditDoc::KColorEditDoc(TQWidget *parent, const char *name) : TQObject(parent, name),
	m_palette(), m_paletteHistory(&m_palette, 0) {
	m_pViewList = new TQPtrList<KColorEditView>();
	m_pViewList->setAutoDelete(true);
}

KColorEditDoc::~KColorEditDoc()
{
}

void KColorEditDoc::addView(KColorEditView *view)
{
  m_pViewList->append(view);
}

void KColorEditDoc::removeView(KColorEditView *view)
{
  m_pViewList->remove(view);
}

void KColorEditDoc::setModified(bool b) {
	m_modified = b;

    emit modified( b );
}

void KColorEditDoc::setAbsFilePath(const TQString &filename)
{
  m_absFilePath=filename;
}

const TQString &KColorEditDoc::absFilePath() const
{
  return m_absFilePath;
}

void KColorEditDoc::setTitle(const TQString &_t)
{
  m_title=_t;
}

const TQString &KColorEditDoc::title() const
{
  return m_title;
}

void KColorEditDoc::slotRedrawAllViews(KColorEditView *sender, bool newDocument) {
 KColorEditView *w;
  if(m_pViewList)
  {
    for(w=m_pViewList->first(); w!=0; w=m_pViewList->next())
    {
      if(w!=sender)
        w->redraw(newDocument);
    }
  }
}

void KColorEditDoc::slotChangeViewMode(bool viewColorNames) {
 KColorEditView *w;
  if(m_pViewList)
  {
    for(w=m_pViewList->first(); w!=0; w=m_pViewList->next())
    {
      w->slotViewColorNames(viewColorNames);
    }
  }
}

bool KColorEditDoc::saveModified()
{
  bool completed=true;

  if(m_modified)
  {
    KColorEditApp *window=(KColorEditApp *) parent();
    int want_save = KMessageBox::warningYesNoCancel(window,
                                         i18n("The current file has been modified.\n"
                                              "Do you want to save it?"), TQString(), KStdGuiItem::save(), i18n("Do Not Save"));
    switch(want_save)
    {
      case KMessageBox::Yes:
           if (title() == i18n("Untitled"))
           {
             completed = window->slotFileSaveAs();
           }
           else
           {
             completed = saveDocument(absFilePath());
       	   };
       	   if(!completed)
							KMessageBox::sorry(0, errorString());
           break;

      case KMessageBox::No:
           completed=true;
           break;

      case KMessageBox::Cancel:
           completed=false;
           break;

      default:
           completed=false;
           break;
    }
  }

  return completed;
}

void KColorEditDoc::closeDocument()
{
  deleteContents();
}

bool KColorEditDoc::newDocument()
{
  deleteContents();
	setModified(false);
  setAbsFilePath( TQDir::homeDirPath() );
  setTitle( i18n("Untitled") );
  setPaletteCursorPos(0);
  setPaletteSelection(0, 0);
  slotRedrawAllViews(0, true);
  return true;
}

bool KColorEditDoc::openDocument(const TQString& filename) {
	if(filename.isEmpty())
		return newDocument();
	else {
		deleteContents();
		TQFileInfo fileInfo(filename);
		setAbsFilePath( fileInfo.absFilePath() );
		if(!m_palette.load( absFilePath() )) {
			setErrorString(m_palette.errorString());
	  		return false;
		}
		setModified(false);
		setTitle( fileInfo.fileName() );
		setPaletteCursorPos(m_palette.length());
		setPaletteSelection(0, 0);
		slotRedrawAllViews(0, true);
        KColorEditApp *window=(KColorEditApp*)parent();
        window->setCaption(m_title);
	}
	return true;
}

bool KColorEditDoc::saveDocument(const TQString& filename) {
	if(!m_palette.save( filename )) {
	 	setErrorString(m_palette.errorString());
	 	return false;
	}
  setModified(false);
  return true;
}

void KColorEditDoc::deleteContents() {
	m_palette.deleteContents();
}

void KColorEditDoc::setErrorString(const TQString& string) {
	m_errorString = string;
}

const TQString& KColorEditDoc::errorString() const {
	return m_errorString;
}

PaletteHistory* KColorEditDoc::paletteHistory() {
	return &m_paletteHistory;
}

void KColorEditDoc::setPaletteCursorPos(const int pos) {
  m_paletteCursorPos = pos;

  emit paletteAvailable( pos < m_palette.length() );
}

int KColorEditDoc::paletteCursorPos() {
	return m_paletteCursorPos;
}

void KColorEditDoc::setPaletteSelection(const int begin, const int end) {
	m_paletteSelectionBegin = begin;
	m_paletteSelectionEnd = end;

    emit selectionChanged( begin, end );
}

int KColorEditDoc::paletteSelectionBegin() const {
	return m_paletteSelectionBegin;
}

int KColorEditDoc::paletteSelectionEnd() const {
	return m_paletteSelectionEnd;
}

void KColorEditDoc::copyToClipboard(Palette& palette) {
	TQString text;
	TQTextOStream stream(&text);
	palette.save(stream, 0, false);
	KApplication::clipboard()->setText(text);

    emit clipboardChanged();
}

void KColorEditDoc::copy() {
	Palette paletteCopy = m_palette.copy(paletteSelectionBegin(),
		paletteSelectionEnd() - paletteSelectionBegin());
	copyToClipboard(paletteCopy);
}

void KColorEditDoc::cut() {
	Palette paletteCut = m_paletteHistory.cut(paletteSelectionBegin(),
		paletteSelectionEnd() - paletteSelectionBegin());
	copyToClipboard(paletteCut);
	setPaletteCursorPos(paletteSelectionBegin());
	setPaletteSelection(0, 0);
	setModified(true);
	slotRedrawAllViews(0);
}

void KColorEditDoc::paste() {
	Palette palettePaste;
	TQString text;
	TQTextIStream stream(&text);
	text = KApplication::clipboard()->text();
	if(palettePaste.load( stream, false )) {
		m_paletteHistory.paste(paletteCursorPos(), palettePaste);
		setPaletteSelection(paletteCursorPos(), paletteCursorPos() +
			palettePaste.length());
		setModified(true);
		slotRedrawAllViews(0);
	}
}

void KColorEditDoc::insert(int index, const Color& color) {
	Palette paletteInsert;
	Color* insertColor = new Color(color);
	paletteInsert.append(insertColor);
	m_paletteHistory.paste(index, paletteInsert);
    setPaletteCursorPos( index );
	setPaletteSelection(0, 0);
	setModified(true);
	slotRedrawAllViews(0);
}

void KColorEditDoc::replace(int index, const Color& color) {
	Palette paletteReplace;
	Color* replaceColor = new Color(color);
	paletteReplace.append(replaceColor);
	m_paletteHistory.replace(index, paletteReplace);
	setPaletteSelection(0, 0);
	setModified(true);
	slotRedrawAllViews(0);
}
#include "kcoloreditdoc.moc"