summaryrefslogtreecommitdiffstats
path: root/src/view.cpp
blob: 898ffb20cf170b1798c50a20c3c1283ac1f35c9b (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
/***************************************************************************
 *   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 "iteminterface.h"
#include "ktechlab.h"
#include "view.h"
#include "viewiface.h"
#include "viewcontainer.h"

#include <kdebug.h>
#include <kiconloader.h>
#include <klocale.h>
#include <ksqueezedtextlabel.h>
#include <tqpainter.h>

//BEGIN KVSSBSep
// Taken from kdebase-3.4.0/kate/app/kateviewspace.cpp, Copyright Anders Lund <anders.lund@lund.tdcadsl.dk>
/*
   "KateViewSpaceStatusBarSeparator"
   A 2 px line to separate the statusbar from the view.
   It is here to compensate for the lack of a frame in the view,
   I think Kate looks very nice this way, as TQScrollView with frame
   looks slightly clumsy...
   Slight 3D effect. I looked for suitable TQStyle props or methods,
   but found none, though maybe it should use TQStyle::PM_DefaultFrameWidth
   for height (TRY!).
   It does look a bit funny with flat styles (Light, .Net) as is,
   but there are on methods to paint panel lines separately. And,
   those styles tends to look funny on their own, as a light line
   in a 3D frame next to a light contents widget is not functional.
   Also, TQStatusBar is up to now completely ignorant to style.
   -anders
*/
class KVSSBSep : public TQWidget {
	public:
		KVSSBSep( View * parent=0) : TQWidget(parent)
		{
			setFixedHeight( 2 );
		}
	protected:
		void paintEvent( TQPaintEvent *e )
		{
			TQPainter p( this );
			p.setPen( colorGroup().shadow() );
			p.drawLine( e->rect().left(), 0, e->rect().right(), 0 );
			p.setPen( ((View*)parentWidget())->isFocused() ? colorGroup().light() : colorGroup().midlight() );
			p.drawLine( e->rect().left(), 1, e->rect().right(), 1 );
		}
};
//END KVSSBSep



//BEGIN class View
View::View( Document *document, ViewContainer *viewContainer, uint viewAreaId, const char *name )
	: TQWidget( viewContainer->viewArea(viewAreaId), name ? name : (const char *)("view_" + TQString::number(viewAreaId)) ),
	  KXMLGUIClient()
{
	m_dcopID = 0;
	m_viewAreaId = viewAreaId;
	m_pDocument = document;
	p_ktechlab = document->ktechlab();
	p_viewContainer = viewContainer;
	m_pViewIface = 0l;
	
	if ( ViewArea * viewArea = viewContainer->viewArea(viewAreaId) )
		viewArea->setView(this);
	
	else
		kdDebug() << k_funcinfo << " viewArea = " << viewArea <<endl;
	
	b_isFocused = false;
	
	m_layout = new TQVBoxLayout(this);
	
	// Don't bother creating statusbar if no ktechlab as we are not a main ktechlab tab
	if (p_ktechlab)
	{
		m_statusBar = new ViewStatusBar(this);
	
		m_layout->addWidget( new KVSSBSep(this) );
		m_layout->addWidget( m_statusBar );
	
		connect( p_ktechlab, TQT_SIGNAL(configurationChanged()), this, TQT_SLOT(slotUpdateConfiguration()) );
	}
}


View::~View()
{
	if (p_ktechlab)
		p_ktechlab->factory()->removeClient(this);
}


KAction * View::action( const TQString & name ) const
{
	KAction * action = actionCollection()->action(name);
	if ( !action )
		kdError() << k_funcinfo << "No such action: " << name << endl;
	return action;
}


DCOPObject * View::dcopObject( ) const
{
	return m_pViewIface;
}


bool View::closeView()
{
	return p_viewContainer->closeViewArea( viewAreaId() );
}


void View::setFocused()
{
	b_isFocused = true;
	p_viewContainer->setActiveViewArea( viewAreaId() );
	
	if ( p_ktechlab )
	{
		p_ktechlab->action("file_save")->setEnabled(true);
		p_ktechlab->action("file_save_as")->setEnabled(true);
		p_ktechlab->action("file_close")->setEnabled(true);
		p_ktechlab->action("file_print")->setEnabled(true);
		p_ktechlab->action("edit_paste")->setEnabled(true);
		p_ktechlab->action("view_split_leftright")->setEnabled(true);
		p_ktechlab->action("view_split_topbottom")->setEnabled(true);
		
		ItemInterface::self()->updateItemActions();
	}
	
	emit viewFocused(this);
}


void View::setUnfocused()
{
	b_isFocused = false;
	emit viewUnfocused();
}


void View::setDCOPID( unsigned id )
{
	if ( m_dcopID == id )
		return;
	
	m_dcopID = id;
	if ( m_pViewIface )
	{
		TQCString docID;
		docID.setNum( document()->dcopID() );
		
		TQCString viewID;
		viewID.setNum( dcopID() );
		
		m_pViewIface->setObjId( "View#" + docID + "." + viewID );
	}
}
//END class View



//BEGIN class ViewStatusBar
ViewStatusBar::ViewStatusBar( View *view )
	: KStatusBar(view)
{
	p_view = view;
	
	m_modifiedLabel = new TQLabel(this);
	addWidget( m_modifiedLabel, 0, false );
	m_fileNameLabel = new KSqueezedTextLabel(this);
	addWidget( m_fileNameLabel, 1, false );
	
	m_modifiedPixmap = KGlobal::iconLoader()->loadIcon( "filesave", KIcon::Small );
	m_unmodifiedPixmap = KGlobal::iconLoader()->loadIcon( "null", KIcon::Small );
	
	connect( view->document(), TQT_SIGNAL(modifiedStateChanged()), this, TQT_SLOT(slotModifiedStateChanged()) );
	connect( view->document(), TQT_SIGNAL(fileNameChanged(const KURL& )), this, TQT_SLOT(slotFileNameChanged(const KURL& )) );
	
	connect( view, TQT_SIGNAL(viewFocused(View* )), this, TQT_SLOT(slotViewFocused(View* )) );
	connect( view, TQT_SIGNAL(viewUnfocused()), this, TQT_SLOT(slotViewUnfocused()) );
	
	slotModifiedStateChanged();
	slotFileNameChanged( view->document()->url() );
}


void ViewStatusBar::slotModifiedStateChanged()
{
	m_modifiedLabel->setPixmap( p_view->document()->isModified() ? m_modifiedPixmap : m_unmodifiedPixmap );
}


void ViewStatusBar::slotFileNameChanged( const KURL &url )
{
	m_fileNameLabel->setText( url.isEmpty() ? i18n("Untitled") : url.fileName(true) );
}


void ViewStatusBar::slotViewFocused( View * )
{
	setPalette(p_view->palette());
}


void ViewStatusBar::slotViewUnfocused()
{
	TQPalette pal( p_view->palette() );
	pal.setColor( TQColorGroup::Background, pal.active().mid() );
	pal.setColor( TQColorGroup::Light, pal.active().midlight() );
	setPalette(pal);
}
//END class ViewStatusBar

#include "view.moc"