summaryrefslogtreecommitdiffstats
path: root/kcoloredit/kcoloredit.cpp
blob: f933bea94c20f287053692ce212f3ab15e858a67 (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
/***************************************************************************
                          kcoloredit.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 QT
#include <tqdir.h>
#include <tqprinter.h>
#include <tqpainter.h>

// include files for KDE
#include <kiconloader.h>
#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tdemenubar.h>
#include <tdelocale.h>
#include <kcolordialog.h>
#include <tdeconfig.h>
#include <kdebug.h>

// application specific includes
#include "kcoloredit.h"
#include "kcoloreditview.h"
#include "kcoloreditdoc.h"
#include "loadpalettedlg.h"
#include "resource.h"
#include <tdepopupmenu.h>
#include <kstatusbar.h>


KColorEditApp::KColorEditApp() : TDEMainWindow(0) {
  config=kapp->config();

  ///////////////////////////////////////////////////////////////////
  // call inits to invoke all other construction parts
  initActions();
  initStatusBar();
  initDocument();
  initView();

  resize(606,400);
  setupGUI();

  readOptions();

  gettingColorFromScreen = false;
}

KColorEditApp::~KColorEditApp() {
}

void KColorEditApp::initActions()
{
  // File actiojns
  KStdAction::openNew( TQT_TQOBJECT(this), TQT_SLOT( slotFileNew() ), actionCollection() );
  KStdAction::open( TQT_TQOBJECT(this), TQT_SLOT( slotFileOpen() ), actionCollection() );
  KStdAction::saveAs( TQT_TQOBJECT(this), TQT_SLOT( slotFileSaveAs() ), actionCollection() );
  KStdAction::close( TQT_TQOBJECT(this), TQT_SLOT( slotClose() ), actionCollection() );
  KStdAction::quit( TQT_TQOBJECT(this), TQT_SLOT( slotQuit() ), actionCollection() );
  m_actSave = KStdAction::save( TQT_TQOBJECT(this), TQT_SLOT( slotFileSave() ),
          actionCollection() );
  m_actRecent = KStdAction::openRecent( TQT_TQOBJECT(this),
          TQT_SLOT( slotFileOpenRecent( const KURL& ) ), actionCollection() );

  ( void ) new TDEAction( i18n("New &Window"), kapp->miniIcon(), TDEShortcut(),
          TQT_TQOBJECT(this), TQT_SLOT( slotFileNewWindow() ), actionCollection(),
          "file_new_window" );

  // Edit actions
  m_actCut = KStdAction::cut( TQT_TQOBJECT(this), TQT_SLOT( slotEditCut() ),
          actionCollection() );
  m_actCopy = KStdAction::copy( TQT_TQOBJECT(this), TQT_SLOT( slotEditCopy() ),
          actionCollection() );
  m_actPaste = KStdAction::paste( TQT_TQOBJECT(this), TQT_SLOT( slotEditPaste() ),
          actionCollection() );

  m_actPaste->setEnabled( false );

  // Color Menu
  m_actNames = new TDEToggleAction( i18n("Show &Color Names"), TDEShortcut(), TQT_TQOBJECT(this),
          TQT_SLOT( slotViewColorNames() ), actionCollection(),
          "color_view_names" );
  m_actNames->setCheckedState(i18n("Hide &Color Names"));
  m_actPalette = new TDEAction( i18n("From &Palette"), TDEShortcut(), TQT_TQOBJECT(this),
          TQT_SLOT( slotColorFromPalette() ), actionCollection(),
          "color_from_palette" );
  ( void ) new TDEAction( i18n("From &Screen"), TDEShortcut(), TQT_TQOBJECT(this),
          TQT_SLOT( slotColorFromScreen() ), actionCollection(),
          "color_from_screen" );
}

void KColorEditApp::initStatusBar()
{
  statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG, 1);
  statusBar()->setItemAlignment( ID_STATUS_MSG, TQt::AlignLeft );
}

void KColorEditApp::initDocument()
{
  doc = new KColorEditDoc(this);
  doc->newDocument();

  connect( doc, TQT_SIGNAL( selectionChanged( int, int ) ),
          TQT_SLOT( slotSelectionChanged( int, int ) ) );
  connect( doc, TQT_SIGNAL( clipboardChanged() ),
          TQT_SLOT( slotClipboardChanged() ) );
  connect( doc, TQT_SIGNAL( modified( bool ) ),
          TQT_SLOT( slotModified( bool ) ) );
  connect( doc, TQT_SIGNAL( paletteAvailable( bool ) ),
          TQT_SLOT( slotPaletteAvailable( bool ) ) );
}

void KColorEditApp::initView()
{
  ////////////////////////////////////////////////////////////////////
  // create the main widget here that is managed by TDEMainWindow's view-region
  // and connect the widget to your document to display document contents.

  view = new KColorEditView(this);
  doc->addView(view);
  setCentralWidget(view);
  setCaption(doc->title());
}

void KColorEditApp::openDocumentFile(const char* _cmdl)
{
  doc->openDocument(_cmdl);
}


KColorEditDoc *KColorEditApp::document() const
{
  return doc;
}

void KColorEditApp::saveOptions()
{
  saveMainWindowSettings( config, "MainWindowSettings" );
  m_actRecent->saveEntries( config );

  config->setGroup("KColorEdit Options");
  config->writeEntry("ColorNames", viewColorNames);
}

void KColorEditApp::readOptions()
{
  applyMainWindowSettings( config, "MainWindowSettings" );
  m_actRecent->loadEntries( config );

  config->setGroup("KColorEdit Options");

  viewColorNames = config->readBoolEntry("ColorNames", false);
  m_actNames->setChecked(viewColorNames);
  doc->slotChangeViewMode(viewColorNames);
}

bool KColorEditApp::queryClose()
{
  return doc->saveModified();
}

bool KColorEditApp::queryExit()
{
  saveOptions();
  return true;
}

/////////////////////////////////////////////////////////////////////
// TQT_SLOT IMPLEMENTATION
/////////////////////////////////////////////////////////////////////

void KColorEditApp::slotSelectionChanged( int begin, int end )
{
  m_actCut->setEnabled( begin != end );
  m_actCopy->setEnabled( begin != end );
}

void KColorEditApp::slotClipboardChanged()
{
  m_actPaste->setEnabled( true );
}

void KColorEditApp::slotModified( bool b )
{
  m_actSave->setEnabled( b );
}

void KColorEditApp::slotPaletteAvailable( bool b )
{
  m_actPalette->setEnabled( b );
}

void KColorEditApp::slotFileNewWindow()
{
  KColorEditApp *new_window= new KColorEditApp();
  new_window->show();
}

void KColorEditApp::slotFileNew()
{
  if(doc->saveModified()) {
    doc->newDocument();

    setCaption(doc->title());
  }
}

void KColorEditApp::slotFileOpen() {
  if(doc->saveModified()) {
    LoadPaletteDlg dialog(this);
    if(dialog.exec()) {
      TQString fileToOpen = dialog.getFileName();
      if(!fileToOpen.isEmpty())
      {
        if(!doc->openDocument( fileToOpen )) {
          KMessageBox::sorry(0, doc->errorString());
        } else {
          setCaption(doc->title());
          m_actRecent->addURL( KURL::fromPathOrURL( fileToOpen ) );
        }
      }
    }
  }
}

void KColorEditApp::slotFileOpenRecent( const KURL & url )
{
  if(doc->saveModified()) {
    doc->openDocument( url.path() );
    setCaption(doc->title());
  }
}

void KColorEditApp::slotFileSave()
{
  if(!doc->saveDocument( doc->absFilePath() ))
      slotFileSaveAs();
      //KMessageBox::sorry(0, doc->getErrorString());
}

bool KColorEditApp::slotFileSaveAs()
{
    bool result = true;

    while(result) {
        TQString newName=KFileDialog::getSaveFileName(lastSavePaletteAsFileDir,
                  "*|" + i18n("All Files"), this, i18n("Save As"));
        if(newName.isEmpty())
            result = false;
        else {
      TQFileInfo saveAsInfo(newName);
      if(!saveAsInfo.exists() ||
               KMessageBox::questionYesNo( this,
                       i18n("A Document with this name already exists.\n"
                            "Do you want to overwrite it?"),
                       i18n("Warning"), i18n("Overwrite"), KStdGuiItem::cancel() ) == KMessageBox::Yes) {
                if(!doc->saveDocument( newName )) {
                    KMessageBox::sorry(0, doc->errorString());
                    result = false;
                } else {
                    doc->setTitle(saveAsInfo.fileName());
                    doc->setAbsFilePath(saveAsInfo.absFilePath());
                    setCaption(doc->title());
                    lastSavePaletteAsFileDir = saveAsInfo.absFilePath();
                    m_actRecent->addURL( KURL( newName ) );
                    break;
                }
            }
        }
    }

    return result;
}

void KColorEditApp::slotClose()
{
  close();
}

void KColorEditApp::slotFilePrint()
{
  TQPrinter printer;
  if (printer.setup(this))
  {
    view->print(&printer);
  }
}

void KColorEditApp::slotQuit()
{
  saveOptions();
  // close the first window, the list makes the next one the first again.
  // This ensures that queryClose() is called on each window to ask for closing
  TDEMainWindow* w;
  if(memberList)
  {
    for(w=memberList->first(); w!=0; w=memberList->next())
    {
      // only close the window if the closeEvent is accepted. If the user presses Cancel on the saveModified() dialog,
      // the window and the application stay open.
      if(!w->close())
  break;
    }
  }
}

void KColorEditApp::slotEditCut()
{
  doc->cut();
}

void KColorEditApp::slotEditCopy()
{
  doc->copy();
}

void KColorEditApp::slotEditPaste()
{
  doc->paste();
}

void KColorEditApp::slotColorFromPalette() {
  view->chooseColor(doc->paletteHistory()->editableStream()->
  color( doc->paletteCursorPos() ));
}

void KColorEditApp::slotColorFromScreen() {
  gettingColorFromScreen = true;
  grabMouse(crossCursor);
  grabKeyboard();
}

void KColorEditApp::slotViewColorNames()
{
  viewColorNames = m_actNames->isChecked();
  doc->slotChangeViewMode(viewColorNames);
}

void KColorEditApp::mouseReleaseEvent(TQMouseEvent* event) {
	if(gettingColorFromScreen) {
		gettingColorFromScreen = false;
		releaseMouse();
		releaseKeyboard();
		TQColor rgbColor =  KColorDialog::grabColor(event->globalPos());
		color.setComponents(rgbColor.red(), rgbColor.green(), rgbColor.blue());
		view->chooseColor(&color);
	} else
		TDEMainWindow::mouseReleaseEvent(event);
}

#include "kcoloredit.moc"