summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/mainwindow/tinykate.cpp
blob: e87464eee65071db9dbc32102164963526e887ea (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
/***************************************************************************
                             tinykate.cpp
                        Tiny KATE mainwindow
                             -------------------
    begin                : November 2002
    copyright            : (C) 2002 by Joseph Wenninger <jowenn@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free softwaSre; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation.                                         *
 *   ONLY VERSION 2 OF THE LICENSE IS APPLICABLE                           *
 *                                                                         *
 ***************************************************************************/

#include "tinykate.h"

#include "katedocument.h"
#include "kglobal.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/ofiledialog.h>
#include <opie2/oresource.h>
#include <qpe/qpeapplication.h>

/* QT */
#include <qaction.h>
#include <qtoolbutton.h>
#include <qmenubar.h>
#include <qmessagebox.h>


using namespace Opie::Ui;
TinyKate::TinyKate( QWidget *parent, const char *name, WFlags f) :
        QMainWindow( parent, name, f )
{
    shutDown=false;
    nextUnnamed=0;
    currentView=0;
    viewCount=0;
    setCaption(tr("TinyKATE"));
    KGlobal::setAppName("TinyKATE");

    QMenuBar *mb = new QMenuBar( this );
    mb->setMargin( 0 );

    tabwidget=new OTabWidget(this);
    setCentralWidget(tabwidget);
    connect(tabwidget,SIGNAL(currentChanged(QWidget*)),this,SLOT(slotCurrentChanged(QWidget*)));

    //FILE ACTIONS
    QPopupMenu *popup = new QPopupMenu( this );

    // Action for creating a new document
    QAction *a = new QAction( tr( "New" ), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
                              QString::null, 0, this, 0 );
    a->addTo( popup );
    connect(a, SIGNAL(activated()), this, SLOT(slotNew()));

    // Action for opening an exisiting document
    a = new QAction( tr( "Open" ), Opie::Core::OResource::loadPixmap( "fileopen", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    a->addTo(popup);
    connect(a, SIGNAL(activated()), this, SLOT(slotOpen()));


    // Action for saving  document
    a = new QAction( tr( "Save" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    a->addTo(popup);
    connect(a, SIGNAL(activated()), this, SLOT(slotSave()));

    // Action for saving document to a new name
    a = new QAction( tr( "Save As" ), Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    a->addTo(popup);
    connect(a, SIGNAL(activated()), this, SLOT(slotSaveAs()));

    // Action for closing the currently active document
    a = new QAction( tr( "Close" ), Opie::Core::OResource::loadPixmap( "quit_icon", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    a->addTo(popup);
    connect(a, SIGNAL(activated()), this, SLOT(slotClose()));


    mb->insertItem(tr("File"),popup);

    //EDIT ACTIONS
    popup = new QPopupMenu( this );
    bool useBigIcon = qApp->desktop()->size().width() > 330;

    // Action for cutting text
    editCut = new QToolButton( 0 );
    editCut->setUsesBigPixmap( useBigIcon );
    editCut->setAutoRaise( true );
    editCut->setIconSet( Opie::Core::OResource::loadPixmap( "cut", Opie::Core::OResource::SmallIcon ) );

    // Action for Copying text
    editCopy = new QToolButton( 0 );
    editCopy->setUsesBigPixmap( useBigIcon );
    editCopy->setAutoRaise( true );
    editCopy->setIconSet( Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon ) );

    // Action for pasting text
    editPaste =  new QToolButton( 0 );
    editPaste->setUsesBigPixmap( useBigIcon );
    editPaste->setAutoRaise( true );
    editPaste->setIconSet( Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon ) );

    // Action for finding text
    editFind = new QAction( tr( "Find..." ), Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    editFind->addTo(popup);

    // Action for replacing text
    editReplace = new QAction( tr( "Replace..." ), QString::null, 0, this, 0 );
    editReplace->addTo(popup);

    // Action for going to a specific line
    editGotoLine = new QAction( tr( "Goto Line..." ), QString::null, 0, this, 0 );
    editGotoLine->addTo(popup);

    // Action for undo
    editUndo = new QToolButton( 0 );
    editUndo->setUsesBigPixmap( useBigIcon );
    editUndo->setAutoRaise( true );
    editUndo->setIconSet( Opie::Core::OResource::loadPixmap( "undo", Opie::Core::OResource::SmallIcon ) );

    // Action for redo
    editRedo = new QToolButton( 0 );
    editRedo->setUsesBigPixmap( useBigIcon );
    editRedo->setAutoRaise( true );
    editRedo->setIconSet( Opie::Core::OResource::loadPixmap( "redo", Opie::Core::OResource::SmallIcon ) );

    mb->insertItem(tr("Edit"),popup);

    //VIEW ACITONS
    popup = new QPopupMenu( this );

    viewIncFontSizes = new QAction( tr( "Font +" ), QString::null, 0, this, 0 );
    viewIncFontSizes->addTo( popup );

    viewDecFontSizes = new QAction( tr( "Font -" ), QString::null, 0, this, 0 );
    viewDecFontSizes->addTo( popup );

    mb->insertItem(tr("View"),popup);

    popup = new QPopupMenu( this );
    mb->insertItem(tr("Utils"),popup);


    mb->insertItem( editCut );
    mb->insertItem( editCopy );
    mb->insertItem( editPaste );
    mb->insertItem( editUndo );
    mb->insertItem(  editRedo );


    //Highlight management
    hlmenu=new QPopupMenu(this);
    HlManager *hlm=HlManager::self();
    for (int i=0;i<hlm->highlights();i++)
    {
        hlmenu->insertItem(hlm->hlName(i),i);
    }
    popup->insertItem(tr("Highlighting"),hlmenu);


    utilSettings = new QAction( tr( "Settings" ),
                                Opie::Core::OResource::loadPixmap( "SettingsIcon", Opie::Core::OResource::SmallIcon ),
                                QString::null, 0, this, 0 );
    utilSettings->addTo( popup);

    if( qApp->argc() > 1) open(qApp->argv()[1]);
    else slotNew();

}

TinyKate::~TinyKate( )
{
    owarn << "TinyKate destructor\n" << oendl;

    if( KGlobal::config() != 0 )
    {
        owarn << "deleting KateConfig object..\n" << oendl;
        delete KGlobal::config();
    }
}

void TinyKate::slotOpen( )
{
    QString filename = OFileDialog::getOpenFileName( OFileSelector::EXTENDED_ALL,
                       QString::null);
    if (!filename.isEmpty())
    {
        open(filename);
    }
}

void TinyKate::open(const QString & filename)
{
    KateDocument *kd= new KateDocument(false, false, this,0,this);
    KTextEditor::View *kv;
    QString realFileName;
    //check if filename is a .desktop file
    if ( filename.find( ".desktop", 0, true ) != -1 ) {
        switch ( QMessageBox::warning( this, tr( "TinyKATE" ),
                                       tr("TinyKATE has detected<BR>you selected a <B>.desktop</B> file.<BR>Open <B>.desktop</B> file or <B>linked</B> file?" ),
                                       tr(".desktop File"),
                                       tr("Linked Document"), 0, 1, 1 ) )
        {
        case 0: //desktop
            realFileName = filename;
            break;
        case 1: //linked
            DocLnk docLnk( filename );
            realFileName = docLnk.file();
            break;
        };
    } else {
	    realFileName = filename;
    }

    QFileInfo fileInfo( realFileName );
    QString filenamed = fileInfo.fileName();
    tabwidget->addTab(kv=kd->createView(tabwidget,"bLAH"),"tinykate/tinykate", filenamed );
    odebug << realFileName << oendl;

    kd->setDocName( filenamed);
    kd->open( realFileName );
    viewCount++;
}

void TinyKate::setDocument(const QString& fileref)
{
    open( fileref );
}

void TinyKate::slotCurrentChanged( QWidget * view)
{
    if (currentView)
    {

        disconnect(editCopy,SIGNAL(clicked()),currentView,SLOT(copy()));
        disconnect(editCut,SIGNAL(clicked()),currentView,SLOT(cut()));
        disconnect(editPaste,SIGNAL(clicked()),currentView,SLOT(paste()));
        disconnect(editUndo,SIGNAL(clicked()),currentView,SLOT(undo()));
        disconnect(editRedo,SIGNAL(clicked()),currentView,SLOT(redo()));
        disconnect(editFind,SIGNAL(activated()),currentView,SLOT(find()));
        disconnect(editReplace,SIGNAL(activated()),currentView,SLOT(replace()));
        disconnect(editGotoLine,SIGNAL(activated()),currentView,SLOT(gotoLine()));
        disconnect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
        disconnect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
        disconnect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
        disconnect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));
    }

    currentView=(KTextEditor::View*)view;

    connect(editCopy,SIGNAL(clicked()),currentView,SLOT(copy()));
    connect(editCut,SIGNAL(clicked()),currentView,SLOT(cut()));
    connect(editPaste,SIGNAL(clicked()),currentView,SLOT(paste()));
    connect(editUndo,SIGNAL(clicked()),currentView,SLOT(undo()));
    connect(editRedo,SIGNAL(clicked()),currentView,SLOT(redo()));
    connect(editFind,SIGNAL(activated()),currentView,SLOT(find()));
    connect(editReplace,SIGNAL(activated()),currentView,SLOT(replace()));
    connect(editGotoLine,SIGNAL(activated()),currentView,SLOT(gotoLine()));
    connect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
    connect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
    connect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
    connect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));

}

void TinyKate::slotNew( )
{
    KateDocument *kd= new KateDocument(false, false, this,0,this);
    KTextEditor::View *kv;
    
    kd->setDocName(tr("Unnamed %1").arg(nextUnnamed++));
    kd->setNewDoc(true);
    tabwidget->addTab(kv=kd->createView(tabwidget,"BLAH"),
                      "tinykate/tinykate",
                      kd->docName());
    viewCount++;
}

bool TinyKate::checkSave() {
    if (currentView==0) return true;

    KateView *kv = (KateView*) currentView;
    if(kv->isModified()) {
        KateDocument *kd = (KateDocument*) kv->document();
        switch( QMessageBox::information( 0, (tr("TinyKATE")),
                                        (tr("Do you want to save\n"
                                            "changes to the document\n"
                                            "%1?\n").arg(kd->docName())),
                                        (tr("Save")), (tr("Don't Save")), (tr("&Cancel")), 2, 2 ) )
        {
            case 0:
            {
                return saveDocument();
            }
            break;
    
            case 1:
            {
                return true;
            }
            break;
    
            default:
            {
                return false;
            }
            break;
        };
    }
    else {
        return true;
    }
}

bool TinyKate::closeDocument()
{
    if (currentView==0) return true;
    KTextEditor::View *dv=currentView;
    if(checkSave()) {
        currentView=0;
        tabwidget->removePage(dv);
        delete dv->document();
        viewCount--;
        if ((!viewCount) && (!shutDown))  slotNew();
        return true;
    }
    else
        return false;
}

void TinyKate::slotClose( )
{
    closeDocument();
}

bool TinyKate::saveDocument()
{
    // feel free to make this how you want
    if (currentView==0) return false;

    //  KateView *kv = (KateView*) currentView;
    KateDocument *kd = (KateDocument*) currentView->document();
    //  odebug << "saving file "+kd->docName() << oendl;
    if( kd->isNewDoc()) {
        return saveDocumentAs();
    }
    else
        return kd->saveFile();
    // FIXME check result of saveFile and show message if failed?
    //    kv->save();
    //    kd->saveFile();
}

bool TinyKate::saveDocumentAs()
{
    if (currentView==0) return false;

    bool result = false;
    KateDocument *kd = (KateDocument*) currentView->document();

    QString filename= OFileDialog::getSaveFileName(OFileSelector::EXTENDED_ALL,
                      QString::null);
    if (!filename.isEmpty())
    {
        odebug << "saving file "+filename << oendl;
        QFileInfo fi(filename);
        if(fi.exists()) {
            if ( QMessageBox::warning(this,tr("TinyKATE"),
                                  tr("The file %1\n"
                                     "already exists.\n\n"
                                     "Are you sure you want to\n"
                                     "overwrite it?")
                                     .arg(fi.fileName()),
                                  tr("Yes"),tr("No"),0,0,1) != 0) {
                return false;
            }
        }
        QString filenamed = fi.fileName();
        kd->setDocFile( filename);
        kd->setDocName( filenamed);
        kd->setNewDoc(false);
        result = kd->saveFile();
        // FIXME check result of saveFile and show message if failed?
        tabwidget->changeTab( tabwidget->currentWidget(), "tinykate/tinykate", filenamed );
    }
    return result;
}

void TinyKate::slotSave()
{
    saveDocument();
}

void TinyKate::slotSaveAs()
{
    saveDocumentAs();
}

void TinyKate::closeEvent(QCloseEvent *e) {
    // Close all documents
    shutDown = true;
    while (currentView!=0)
    {
        if(!closeDocument()) {
            // User cancelled
            shutDown=false;
            break;
        }
    }

    if(shutDown)
        e->accept();
    else
        e->ignore();
}