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
|
/***************************************************************************
* Copyright (C) 2005 by David Saxton *
* david@bluehaze.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 "document.h"
#include "documentiface.h"
#include "ktechlab.h"
#include "projectmanager.h"
#include "view.h"
#include "viewcontainer.h"
#include <kfiledialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <ktabwidget.h>
Document::Document( const TQString &caption, KTechlab *ktechlab, const char *name )
: TQObject( ktechlab, name ),
b_modified(false),
p_ktechlab(ktechlab),
p_activeView(0l),
m_caption(caption),
m_bAddToProjectOnSave(false),
m_pDocumentIface(0l),
m_dcopID(0),
m_nextViewID(0),
m_bDeleted(false)
{
if (p_ktechlab)
connect( p_ktechlab, TQT_SIGNAL(configurationChanged()), this, TQT_SLOT(slotUpdateConfiguration()) );
}
Document::~Document()
{
m_bDeleted = true;
ViewList viewsToDelete = m_viewList;
const ViewList::iterator end = viewsToDelete.end();
for ( ViewList::iterator it = viewsToDelete.begin(); it != end; ++it )
(*it)->deleteLater();
}
void Document::handleNewView( View *view )
{
if ( !view || m_viewList.tqcontains(view) )
return;
m_viewList.append(view);
view->setDCOPID(m_nextViewID++);
view->setCaption(m_caption);
connect( view, TQT_SIGNAL(destroyed(TQObject* )), this, TQT_SLOT(slotViewDestroyed(TQObject* )) );
connect( view, TQT_SIGNAL(viewFocused(View* )), this, TQT_SLOT(slotViewFocused(View* )) );
connect( view, TQT_SIGNAL(viewUnfocused()), this, TQT_SIGNAL(viewUnfocused()) );
view->show();
view->setFocused();
}
void Document::slotViewDestroyed( TQObject *obj )
{
View *view = static_cast<View*>(TQT_TQWIDGET(obj));
m_viewList.remove(view);
if ( p_activeView == (TQGuardedPtr<View>)view )
{
p_activeView = 0l;
emit viewUnfocused();
}
if ( m_viewList.isEmpty() )
deleteLater();
}
void Document::slotViewFocused(View *view)
{
if (!view)
return;
p_activeView = view;
emit viewFocused(view);
}
void Document::setCaption( const TQString &caption )
{
m_caption = caption;
const ViewList::iterator end = m_viewList.end();
for ( ViewList::iterator it = m_viewList.begin(); it != end; ++it )
(*it)->setCaption(caption);
}
bool Document::getURL( const TQString &types )
{
KURL url = KFileDialog::getSaveURL( TQString(), types, p_ktechlab, i18n("Save Location"));
if ( url.isEmpty() )
return false;
if ( TQFile::exists( url.path() ) )
{
int query = KMessageBox::warningYesNo( p_ktechlab,
i18n( "A file named \"%1\" already exists. Are you sure you want to overwrite it?" ).tqarg( url.fileName() ),
i18n( "Overwrite File?" ),
i18n( "Overwrite" ),
KStdGuiItem::cancel() );
if ( query == KMessageBox::No )
return false;
}
setURL(url);
return true;
}
bool Document::fileClose()
{
if ( isModified() )
{
// If the filename is empty then it must be an untitled file.
TQString name = m_url.fileName().isEmpty() ? caption() : m_url.fileName();
if ( ViewContainer * viewContainer = (activeView() ? activeView()->viewContainer() : 0l) )
p_ktechlab->tabWidget()->setCurrentPage( p_ktechlab->tabWidget()->indexOf(viewContainer) );
int choice = KMessageBox::warningYesNoCancel( p_ktechlab,
i18n("The document \'%1\' has been modified.\nDo you want to save it?").tqarg(name),
i18n("Save Document?"),
i18n("Save"),
i18n("Discard") );
if ( choice == KMessageBox::Cancel )
return false;
if ( choice == KMessageBox::Yes )
fileSave();
}
deleteLater();
return true;
}
void Document::setModified( bool modified )
{
if ( b_modified == modified )
return;
b_modified = modified;
if (!m_bDeleted)
emit modifiedStateChanged();
}
void Document::setURL( const KURL &url )
{
if ( m_url == url )
return;
bool wasEmpty = m_url.isEmpty();
m_url = url;
if ( wasEmpty && m_bAddToProjectOnSave && ProjectManager::self()->currentProject() )
ProjectManager::self()->currentProject()->addFile(m_url);
emit fileNameChanged(url);
if (p_ktechlab)
{
p_ktechlab->addRecentFile(url);
p_ktechlab->requestUpdateCaptions();
}
}
DCOPObject * Document::dcopObject( ) const
{
return m_pDocumentIface;
}
void Document::setDCOPID( unsigned id )
{
if ( m_dcopID == id )
return;
m_dcopID = id;
if ( m_pDocumentIface )
{
TQCString docID;
docID.setNum( dcopID() );
m_pDocumentIface->setObjId( "Document#" + docID );
}
}
#include "document.moc"
|