Diffstat (limited to 'noncore/graphics/opie-eye/gui/iconview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/graphics/opie-eye/gui/iconview.cpp | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/noncore/graphics/opie-eye/gui/iconview.cpp b/noncore/graphics/opie-eye/gui/iconview.cpp new file mode 100644 index 0000000..0b80012 --- a/dev/null +++ b/noncore/graphics/opie-eye/gui/iconview.cpp | |||
@@ -0,0 +1,296 @@ | |||
1 | /* | ||
2 | * GPLv2 zecke@handhelds.org | ||
3 | * No WArranty... | ||
4 | */ | ||
5 | |||
6 | #include "iconview.h" | ||
7 | |||
8 | #include <lib/imagecache.h> | ||
9 | |||
10 | #include <iface/dirview.h> | ||
11 | #include <iface/dirlister.h> | ||
12 | |||
13 | #include <qpe/config.h> | ||
14 | #include <qpe/resource.h> | ||
15 | #include <qpe/qpemessagebox.h> | ||
16 | #include <qpe/ir.h> | ||
17 | #include <qpe/qcopenvelope_qws.h> | ||
18 | |||
19 | #include <qiconview.h> | ||
20 | #include <qlabel.h> | ||
21 | #include <qhbox.h> | ||
22 | #include <qcombobox.h> | ||
23 | #include <qdir.h> | ||
24 | #include <qapplication.h> | ||
25 | #include <qmainwindow.h> | ||
26 | #include <qtimer.h> | ||
27 | #include <qstyle.h> | ||
28 | |||
29 | |||
30 | |||
31 | namespace { | ||
32 | QPixmap* _dirPix = 0; | ||
33 | QPixmap* _unkPix = 0; | ||
34 | class IconViewItem : public QIconViewItem { | ||
35 | public: | ||
36 | IconViewItem( QIconView*, const QString& path, const QString& name, bool isDir = false); | ||
37 | QPixmap* pixmap()const; | ||
38 | QString path()const { return m_path; } | ||
39 | bool isDir()const { return m_isDir; } | ||
40 | void setText( const QString& ); | ||
41 | private: | ||
42 | mutable QPixmap* m_pix; | ||
43 | QString m_path; | ||
44 | bool m_isDir : 1; | ||
45 | bool m_noInfo :1; | ||
46 | }; | ||
47 | |||
48 | |||
49 | /* | ||
50 | * If we request an Image or String | ||
51 | * we add it to the map | ||
52 | */ | ||
53 | QMap<QString, IconViewItem*> g_stringInf; | ||
54 | QMap<QString, IconViewItem*> g_stringPix; | ||
55 | |||
56 | IconViewItem::IconViewItem( QIconView* view,const QString& path, | ||
57 | const QString& name, bool isDir ) | ||
58 | : QIconViewItem( view ), m_path( path ), m_isDir( isDir ), | ||
59 | m_noInfo( false ) | ||
60 | { | ||
61 | QIconViewItem::setText( name ); | ||
62 | if ( isDir && !_dirPix ) | ||
63 | _dirPix = new QPixmap( Resource::loadPixmap("advancedfm/FileBrowser")); | ||
64 | else if ( !isDir && !_unkPix ) | ||
65 | _unkPix = new QPixmap( Resource::loadPixmap( "UnknownDocument" ) ); | ||
66 | } | ||
67 | inline QPixmap* IconViewItem::pixmap()const { | ||
68 | if ( m_isDir ) | ||
69 | return _dirPix; | ||
70 | else{ | ||
71 | if (!m_noInfo && !g_stringInf.contains( m_path ) ) { | ||
72 | currentView()->dirLister()->imageInfo( m_path ); | ||
73 | g_stringInf.insert( m_path, const_cast<IconViewItem*>(this)); | ||
74 | } | ||
75 | |||
76 | m_pix = PPixmapCache::self()->cachedImage( m_path, 64, 64 ); | ||
77 | if ( !m_pix && !g_stringPix.contains( m_path )) { | ||
78 | currentView()->dirLister()->thumbNail( m_path, 64, 64 ); | ||
79 | g_stringPix.insert( m_path, const_cast<IconViewItem*>(this)); | ||
80 | } | ||
81 | return m_pix ? m_pix : _unkPix; | ||
82 | } | ||
83 | } | ||
84 | inline void IconViewItem::setText( const QString& str ) { | ||
85 | QString text = QIconViewItem::text()+"\n"+str; | ||
86 | m_noInfo = true; | ||
87 | QIconViewItem::setText( text ); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | |||
92 | PIconView::PIconView( QWidget* wid, Config* cfg ) | ||
93 | : QVBox( wid ), m_cfg( cfg ) | ||
94 | { | ||
95 | { | ||
96 | QCopEnvelope( "QPE/Application/opie-eye_slave", "foo()" ); | ||
97 | } | ||
98 | m_path = QDir::homeDirPath(); | ||
99 | |||
100 | QHBox *hbox = new QHBox( this ); | ||
101 | QLabel* lbl = new QLabel( hbox ); | ||
102 | lbl->setText( tr("View as" ) ); | ||
103 | |||
104 | m_views = new QComboBox( hbox, "View As" ); | ||
105 | connect( m_views, SIGNAL(activated(int)), | ||
106 | this, SLOT(slotViewChanged(int)) ); | ||
107 | |||
108 | m_view= new QIconView( this ); | ||
109 | connect(m_view, SIGNAL(clicked(QIconViewItem*) ), | ||
110 | this, SLOT(slotClicked(QIconViewItem*)) ); | ||
111 | |||
112 | m_view->setArrangement( QIconView::LeftToRight ); | ||
113 | m_view->setItemTextPos( QIconView::Right ); | ||
114 | |||
115 | |||
116 | int dw = QApplication::desktop()->width(); | ||
117 | int viewerWidth = dw-style().scrollBarExtent().width(); | ||
118 | m_view->setGridX( viewerWidth-2*m_view->spacing() ); | ||
119 | m_view->setGridY( fontMetrics().height()*2+40 ); | ||
120 | loadViews(); | ||
121 | slotViewChanged( m_views->currentItem() ); | ||
122 | } | ||
123 | |||
124 | PIconView::~PIconView() { | ||
125 | } | ||
126 | |||
127 | void PIconView::slotDirUp() { | ||
128 | QDir dir( m_path ); | ||
129 | dir.cdUp(); | ||
130 | slotChangeDir( dir.absPath() ); | ||
131 | |||
132 | } | ||
133 | |||
134 | void PIconView::slotChangeDir(const QString& path) { | ||
135 | if ( !currentView() ) | ||
136 | return; | ||
137 | |||
138 | PDirLister *lister = currentView()->dirLister(); | ||
139 | if (!lister ) | ||
140 | return; | ||
141 | |||
142 | lister->setStartPath( path ); | ||
143 | m_path = lister->currentPath(); | ||
144 | |||
145 | m_view->clear(); | ||
146 | addFolders( lister->folders() ); | ||
147 | addFiles( lister->files() ); | ||
148 | |||
149 | // looks ugly | ||
150 | static_cast<QMainWindow*>(parent())->setCaption( QObject::tr("%1 - O View", "Name of the dir").arg( m_path ) ); | ||
151 | } | ||
152 | |||
153 | QString PIconView::currentFileName(bool &isDir)const { | ||
154 | isDir = false; | ||
155 | QIconViewItem* _it = m_view->currentItem(); | ||
156 | if ( !_it ) | ||
157 | return QString::null; | ||
158 | |||
159 | IconViewItem* it = static_cast<IconViewItem*>( _it ); | ||
160 | isDir = it->isDir(); | ||
161 | return it->path(); | ||
162 | } | ||
163 | |||
164 | void PIconView::slotTrash() { | ||
165 | bool isDir; | ||
166 | QString pa = currentFileName( isDir ); | ||
167 | if ( isDir && pa.isEmpty() ) | ||
168 | return; | ||
169 | |||
170 | if (!QPEMessageBox::confirmDelete( this, | ||
171 | tr("Delete Image" ), | ||
172 | tr("the Image %1" ).arg(pa))) | ||
173 | return | ||
174 | |||
175 | |||
176 | currentView()->dirLister()->deleteImage( pa ); | ||
177 | delete m_view->currentItem(); | ||
178 | } | ||
179 | void PIconView::loadViews() { | ||
180 | ViewMap::Iterator it; | ||
181 | ViewMap* map = viewMap(); | ||
182 | for ( it = map->begin(); it != map->end(); ++it ) | ||
183 | m_views->insertItem( QObject::tr(it.key() ) ); | ||
184 | } | ||
185 | |||
186 | void PIconView::resetView() { | ||
187 | slotViewChanged(m_views->currentItem()); | ||
188 | } | ||
189 | |||
190 | void PIconView::slotViewChanged( int i) { | ||
191 | if (!m_views->count() ) { | ||
192 | setCurrentView( 0l); | ||
193 | return; | ||
194 | } | ||
195 | |||
196 | PDirView* cur = currentView(); | ||
197 | delete cur; | ||
198 | QString str = m_views->text(i); | ||
199 | cur = (*(*viewMap())[str])(*m_cfg); | ||
200 | setCurrentView( cur ); | ||
201 | |||
202 | /* connect to the signals of the lister */ | ||
203 | PDirLister* lis = cur->dirLister(); | ||
204 | connect(lis, SIGNAL(sig_thumbInfo(const QString&, const QString& )), | ||
205 | this, SLOT( slotThumbInfo(const QString&, const QString&))); | ||
206 | connect(lis, SIGNAL( sig_thumbNail(const QString&, const QPixmap&)), | ||
207 | this, SLOT(slotThumbNail(const QString&, const QPixmap&))); | ||
208 | connect(lis, SIGNAL(sig_start()), | ||
209 | this, SLOT(slotStart())); | ||
210 | connect(lis, SIGNAL(sig_end()) , | ||
211 | this, SLOT(slotEnd()) ); | ||
212 | |||
213 | |||
214 | /* reload now */ | ||
215 | QTimer::singleShot( 0, this, SLOT(slotReloadDir())); | ||
216 | } | ||
217 | |||
218 | |||
219 | void PIconView::slotReloadDir() { | ||
220 | slotChangeDir( m_path ); | ||
221 | } | ||
222 | |||
223 | |||
224 | void PIconView::addFolders( const QStringList& lst) { | ||
225 | QStringList::ConstIterator it; | ||
226 | |||
227 | for(it=lst.begin(); it != lst.end(); ++it ) { | ||
228 | (void)new IconViewItem( m_view, m_path+"/"+(*it), (*it), true ); | ||
229 | } | ||
230 | |||
231 | } | ||
232 | |||
233 | void PIconView::addFiles( const QStringList& lst) { | ||
234 | QStringList::ConstIterator it; | ||
235 | for (it=lst.begin(); it!= lst.end(); ++it ) | ||
236 | (void)new IconViewItem( m_view, m_path+"/"+(*it), (*it) ); | ||
237 | |||
238 | } | ||
239 | |||
240 | void PIconView::slotClicked(QIconViewItem* _it) { | ||
241 | if(!_it ) | ||
242 | return; | ||
243 | |||
244 | IconViewItem* it = static_cast<IconViewItem*>(_it); | ||
245 | if( it->isDir() ) | ||
246 | slotChangeDir( it->path() ); | ||
247 | else // view image | ||
248 | ; | ||
249 | } | ||
250 | |||
251 | void PIconView::slotThumbInfo( const QString& _path, const QString& str ) { | ||
252 | if ( g_stringInf.contains( _path ) ) { | ||
253 | IconViewItem* item = g_stringInf[_path]; | ||
254 | item->setText( str ); | ||
255 | item->repaint(); | ||
256 | g_stringInf.remove( _path ); | ||
257 | } | ||
258 | } | ||
259 | void PIconView::slotThumbNail(const QString& _path, const QPixmap &pix) { | ||
260 | if ( g_stringPix.contains( _path ) ) { | ||
261 | IconViewItem* item = g_stringPix[_path]; | ||
262 | PPixmapCache::self()->insertImage( _path, pix, 64, 64 ); | ||
263 | item->repaint(); | ||
264 | g_stringPix.remove( _path ); | ||
265 | } | ||
266 | } | ||
267 | |||
268 | |||
269 | void PIconView::slotRename() { | ||
270 | |||
271 | } | ||
272 | |||
273 | void PIconView::slotBeam() { | ||
274 | bool isDir; | ||
275 | QString pa = currentFileName( isDir ); | ||
276 | if ( isDir && pa.isEmpty() ) | ||
277 | return; | ||
278 | |||
279 | Ir* ir = new Ir( this ); | ||
280 | connect( ir, SIGNAL(done(Ir*)), | ||
281 | this, SLOT(slotBeamDone(Ir*))); | ||
282 | ir->send(pa, tr( "Image" ) ); | ||
283 | |||
284 | } | ||
285 | |||
286 | void PIconView::slotBeamDone( Ir* ir) { | ||
287 | delete ir; | ||
288 | } | ||
289 | |||
290 | void PIconView::slotStart() { | ||
291 | m_view->setUpdatesEnabled( false ); | ||
292 | } | ||
293 | |||
294 | void PIconView::slotEnd() { | ||
295 | m_view->setUpdatesEnabled( true ); | ||
296 | } | ||