summaryrefslogtreecommitdiffstats
path: root/src/gvdirpart/gvdirpart.cpp
blob: 1fd7630a0a2a60f7fb1c08be7634e3b9b6638c09 (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
/*
Copyright 2004 Jonathan Riddell <jr@jriddell.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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.

*/
#include "gvdirpart.moc"

#include <tqcursor.h>
#include <tqfile.h>
#include <tqsplitter.h>
#include <tqvaluelist.h>

#include <kdebug.h>
#include <tdeversion.h>
#include <kaction.h>
#include <kicontheme.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kstandarddirs.h>
#include <tdeparts/browserextension.h>
#include <tdeparts/genericfactory.h>
#include <tdeio/job.h>

#include <gvcore/fileoperation.h>
#include <gvcore/archive.h>
#include <gvcore/cache.h>
#include <gvcore/document.h>
#include <gvcore/fileviewbase.h>
#include <gvcore/fileviewcontroller.h>
#include <gvcore/printdialog.h>
#include <gvcore/imageview.h>
#include <gvcore/slideshow.h>
#include <gvcore/thumbnailloadjob.h>

#include "config.h"
#include "gvdirpartconfig.h"

namespace Gwenview {

// For now let's duplicate
const char CONFIG_CACHE_GROUP[]="cache";


//Factory Code
typedef KParts::GenericFactory<GVDirPart> GVDirFactory;
K_EXPORT_COMPONENT_FACTORY( libgvdirpart /*library name*/, GVDirFactory )

GVDirPart::GVDirPart(TQWidget* parentWidget, const char* /*widgetName*/, TQObject* parent, const char* name,
		     const TQStringList &) : KParts::ReadOnlyPart( parent, name )  {
	GVDirFactory::instance()->iconLoader()->addAppDir( "gwenview");
	setInstance( GVDirFactory::instance() );
	TDEGlobal::locale()->insertCatalogue("gwenview");
	TDEGlobal::locale()->setActiveCatalogue("gwenview");

	mBrowserExtension = new GVDirPartBrowserExtension(this);

	mSplitter = new TQSplitter(Qt::Horizontal, parentWidget, "gwenview-kpart-splitter");
	mSplitter->setFocusPolicy(TQ_ClickFocus);
	mSplitter->setOpaqueResize(true);

	// Create the widgets
	mDocument = new Document(this);
	mFileViewController = new FileViewController(mSplitter, actionCollection());
	int width=GVDirPartConfig::fileViewWidth();
	if (width!=-1) {
		mFileViewController->resize(width, 10);
	}
	mImageView = new ImageView(mSplitter, mDocument, actionCollection());
	mSplitter->setResizeMode(mFileViewController, TQSplitter::KeepSize);

	mSlideShow = new SlideShow(mDocument);

	setWidget(mSplitter);

	KStdAction::saveAs( mDocument, TQT_SLOT(saveAs()), actionCollection(), "saveAs" );
	new KAction(i18n("Rotate &Left"), "rotate_ccw", CTRL + Key_L, this, TQT_SLOT(rotateLeft()), actionCollection(), "rotate_left");
	new KAction(i18n("Rotate &Right"), "rotate_cw", CTRL + Key_R, this, TQT_SLOT(rotateRight()), actionCollection(), "rotate_right");

	connect(mFileViewController, TQT_SIGNAL(requestContextMenu(const TQPoint&, bool)),
		mBrowserExtension, TQT_SLOT(openFileViewContextMenu(const TQPoint&, bool)) );

	connect(mFileViewController, TQT_SIGNAL(urlChanged(const KURL&)),
		mDocument, TQT_SLOT(setURL(const KURL&)) );

	connect(mFileViewController, TQT_SIGNAL(directoryChanged(const KURL&)),
		this, TQT_SLOT(directoryChanged(const KURL&)) );

	connect(mFileViewController, TQT_SIGNAL(selectionChanged()),
		mBrowserExtension, TQT_SLOT(updateActions()) );

	connect(mImageView, TQT_SIGNAL(requestContextMenu(const TQPoint&)),
		mBrowserExtension, TQT_SLOT(openImageViewContextMenu(const TQPoint&)) );

	connect(mSlideShow, TQT_SIGNAL(nextURL(const KURL&)),
		this, TQT_SLOT(slotSlideShowChanged(const KURL&)) );

	connect(mDocument, TQT_SIGNAL(loaded(const KURL&)),
		this, TQT_SLOT(loaded(const KURL&)) );

	// For wheel browsing
	connect(mImageView, TQT_SIGNAL(selectPrevious()),
		mFileViewController, TQT_SLOT(slotSelectPrevious()) );
	connect(mImageView, TQT_SIGNAL(selectNext()),
		mFileViewController, TQT_SLOT(slotSelectNext()) );

	mToggleSlideShow = new KToggleAction(i18n("Slide Show..."), "slideshow", 0, this, TQT_SLOT(toggleSlideShow()), actionCollection(), "slideshow");
	mToggleSlideShow->setCheckedState( i18n("Stop Slide Show" ));

	setXMLFile( "gvdirpart/gvdirpart.rc" );
	mBrowserExtension->updateActions();
}

GVDirPart::~GVDirPart() {
	GVDirPartConfig::setFileViewWidth(mFileViewController->width());
	GVDirPartConfig::writeConfig();
	delete mSlideShow;
}


void GVDirPart::partActivateEvent(KParts::PartActivateEvent* event) {
	if (event->activated()) {
		TDEConfig* config=new TDEConfig("gwenviewrc");
		Cache::instance()->readConfig(config,CONFIG_CACHE_GROUP);
		delete config;
	}
}


TDEAboutData* GVDirPart::createAboutData() {
	TDEAboutData* aboutData = new TDEAboutData( "gvdirpart", I18N_NOOP("GVDirPart"),
						"0.1", I18N_NOOP("Image Browser"),
						TDEAboutData::License_GPL,
						"(c) 2004, Jonathan Riddell <jr@jriddell.org>");
	return aboutData;
}

bool GVDirPart::openFile() {
	//unused because openURL implemented

	//mDocument->setFilename(mFile);
	return true;
}

bool GVDirPart::openURL(const KURL& url) {
	if (!url.isValid()) {
		return false;
	}

	emit started( 0 );
	m_url = url;
	m_url.adjustPath(1);

	emit setWindowCaption( m_url.prettyURL() );
	mFileViewController->setDirURL(m_url);

	return true;
}

void GVDirPart::loaded(const KURL& url) {
	TQString caption = url.filename();
	if( !mDocument->image().isNull())
		caption += TQString(" %1 x %2").arg(mDocument->width()).arg(mDocument->height());
	emit setWindowCaption(caption);
	emit completed();
}

KURL GVDirPart::pixmapURL() {
	return mDocument->url();
}

void GVDirPart::toggleSlideShow() {
	if (mToggleSlideShow->isChecked()) {
        KURL::List list;
        KFileItemListIterator it( *mFileViewController->currentFileView()->items() );
        for ( ; it.current(); ++it ) {
            KFileItem* item=it.current();
            if (!item->isDir() && !Archive::fileItemIsArchive(item)) {
                list.append(item->url());
            }
        }
        if (list.count()==0) {
			mToggleSlideShow->setChecked(false);
            return;
        }
		//FIXME turn on full screen here (anyone know how?)
		mSlideShow->start(list);
	} else {
		//FIXME turn off full screen here
		mSlideShow->stop();
	}
}

void GVDirPart::print() {
	KPrinter printer;
	if ( !mDocument->filename().isEmpty() ) {
		printer.setDocName( m_url.filename() );
		KPrinter::addDialogPage( new PrintDialogPage( mDocument, mImageView, "GV page"));

		if (printer.setup(mImageView, TQString(), true)) {
			mDocument->print(&printer);
		}
	}
}

void GVDirPart::rotateLeft() {
	mDocument->transform(ImageUtils::ROT_270);
}

void GVDirPart::rotateRight() {
	mDocument->transform(ImageUtils::ROT_90);
}

void GVDirPart::directoryChanged(const KURL& dirURL) {
	if( dirURL == m_url ) return;
	emit mBrowserExtension->openURLRequest(dirURL);
}

void GVDirPart::slotSlideShowChanged(const KURL& url) {
	mDocument->setURL( url );
	mFileViewController->setFileNameToSelect( url.filename());
}


/***** GVDirPartBrowserExtension *****/

GVDirPartBrowserExtension::GVDirPartBrowserExtension(GVDirPart* viewPart, const char* name)
	:KParts::BrowserExtension(viewPart, name) {
	mGVDirPart = viewPart;
	emit enableAction("print", true );
}

GVDirPartBrowserExtension::~GVDirPartBrowserExtension() {
}

void GVDirPartBrowserExtension::updateActions() {
	bool somethingSelected = mGVDirPart->fileViewController()->selectionSize() != 0;
	emit enableAction("trash", somethingSelected);
	emit enableAction("del", somethingSelected);
}

void GVDirPartBrowserExtension::del() {
	FileViewController* fv = mGVDirPart->fileViewController();
	FileOperation::realDelete(fv->selectedURLs(), fv);
	
}

void GVDirPartBrowserExtension::trash() {
	FileViewController* fv = mGVDirPart->fileViewController();
	FileOperation::trash(fv->selectedURLs(), fv);
}


void GVDirPartBrowserExtension::openFileViewContextMenu(const TQPoint& pos, bool onItem) {
    if (onItem) {
        const KFileItemList* items = mGVDirPart->fileViewController()->currentFileView()->selectedItems();
        emit popupMenu(pos, *items);
    } else {
        emit popupMenu(pos, mGVDirPart->fileViewController()->dirURL(), 0);
    }
}


void GVDirPartBrowserExtension::openImageViewContextMenu(const TQPoint& pos) {
	KURL url=mGVDirPart->url();
	TQString mimeType=KMimeType::findByURL(url)->name();
	emit popupMenu(pos, url, mimeType);
}


void GVDirPartBrowserExtension::print() {
	mGVDirPart->print();
}

} // namespace