Diffstat (limited to 'noncore/unsupported/showimg/ImageFileSelector.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/unsupported/showimg/ImageFileSelector.cpp | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/noncore/unsupported/showimg/ImageFileSelector.cpp b/noncore/unsupported/showimg/ImageFileSelector.cpp new file mode 100644 index 0000000..53cc883 --- a/dev/null +++ b/noncore/unsupported/showimg/ImageFileSelector.cpp | |||
@@ -0,0 +1,262 @@ | |||
1 | #include "ImageFileSelector.h" | ||
2 | |||
3 | /* OPIE */ | ||
4 | #include <opie2/odebug.h> | ||
5 | #include <qpe/qpeapplication.h> | ||
6 | using namespace Opie::Core; | ||
7 | |||
8 | /* QT */ | ||
9 | #include <qlabel.h> | ||
10 | #include <qfileinfo.h> | ||
11 | |||
12 | /* STD */ | ||
13 | #include <stdlib.h> | ||
14 | |||
15 | ThumbWidget::ThumbWidget(QPixmap p,QString text,const DocLnk& f,QWidget *parent,int w) | ||
16 | : QWidget( parent ),fl(f) | ||
17 | { | ||
18 | setBackgroundMode(NoBackground); | ||
19 | if ( w!=-1 ) | ||
20 | setMinimumSize(w,p.height()+24); | ||
21 | else | ||
22 | setMinimumSize(p.width(),p.height()+24); | ||
23 | description=new QLabel(text,this); | ||
24 | description->setBackgroundColor(colorGroup().base()); | ||
25 | description->setAlignment(AlignCenter); | ||
26 | description->setGeometry(0,height()-24,width(),24); | ||
27 | pixmap=p; | ||
28 | } | ||
29 | |||
30 | void ThumbWidget::resizeEvent(QResizeEvent *) | ||
31 | { | ||
32 | description->setGeometry(0,height()-24,width(),24); | ||
33 | } | ||
34 | |||
35 | void ThumbWidget::paintEvent( QPaintEvent *e ) | ||
36 | { | ||
37 | QPainter painter(this); | ||
38 | |||
39 | painter.setClipRect(e->rect()); | ||
40 | painter.fillRect(0,0,width(),height(),QColor(255,255,255)); | ||
41 | painter.drawPixmap((width() - pixmap.width()) / 2,0, pixmap); | ||
42 | |||
43 | } | ||
44 | |||
45 | void ThumbWidget::mouseReleaseEvent(QMouseEvent* ) | ||
46 | { | ||
47 | emit clicked(fl); | ||
48 | } | ||
49 | |||
50 | |||
51 | |||
52 | ImageFileSelectorItem::ImageFileSelectorItem( QListView *parent, const DocLnk &f) | ||
53 | : QListViewItem( parent ), fl( f ) | ||
54 | { | ||
55 | setText( 0, f.name() ); | ||
56 | QFileInfo fi(f.file()); | ||
57 | setText( 1, (fi.extension()).upper() ); | ||
58 | setPixmap( 0, f.pixmap() ); | ||
59 | |||
60 | |||
61 | } | ||
62 | |||
63 | |||
64 | ImageFileSelectorItem::~ImageFileSelectorItem() | ||
65 | { | ||
66 | |||
67 | } | ||
68 | |||
69 | |||
70 | ImageFileSelector::ImageFileSelector( CURRENT_VIEW scv,QWidget *parent,const char * ) | ||
71 | : QWidgetStack(parent) | ||
72 | { | ||
73 | |||
74 | detailed=new QListView(this); | ||
75 | |||
76 | detailed->addColumn (tr("Title")); | ||
77 | detailed->addColumn (tr("Type")); | ||
78 | detailed->setAllColumnsShowFocus( true ); | ||
79 | |||
80 | tList.setAutoDelete(true); | ||
81 | |||
82 | thumb =new QScrollView(this); | ||
83 | thumb->setVScrollBarMode (QScrollView::Auto ); | ||
84 | thumb->viewport()->setBackgroundColor(colorGroup().base()); | ||
85 | |||
86 | background=new QWidget(0); | ||
87 | background->setBackgroundColor(colorGroup().base()); | ||
88 | thumb->addChild(background); | ||
89 | gl = new QGridLayout(background,1,2,4,4); | ||
90 | |||
91 | |||
92 | |||
93 | connect( detailed, SIGNAL( mouseButtonClicked(int,QListViewItem*,const QPoint&,int) ), | ||
94 | this, SLOT( fileClicked(int,QListViewItem*,const QPoint&,int) ) ); | ||
95 | connect( detailed, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ), | ||
96 | this, SLOT( filePressed(int,QListViewItem*,const QPoint&,int) ) ); | ||
97 | connect( detailed, SIGNAL( returnPressed(QListViewItem*) ), | ||
98 | this, SLOT( fileClicked(QListViewItem*) ) ); | ||
99 | |||
100 | cView=UNKNOWN; | ||
101 | setView(scv); | ||
102 | reread(); | ||
103 | |||
104 | } | ||
105 | ImageFileSelector::~ImageFileSelector() | ||
106 | { | ||
107 | |||
108 | } | ||
109 | |||
110 | |||
111 | void ImageFileSelector::switchView() | ||
112 | { | ||
113 | CURRENT_VIEW v=cView; | ||
114 | |||
115 | if ( v==DETAILED ) | ||
116 | v=THUMBNAIL; | ||
117 | else | ||
118 | v=DETAILED; | ||
119 | setView(v); | ||
120 | } | ||
121 | |||
122 | void ImageFileSelector::setView(CURRENT_VIEW v) | ||
123 | { | ||
124 | |||
125 | if ( v==cView ) | ||
126 | return; | ||
127 | cView=v; | ||
128 | |||
129 | if ( cView!=DETAILED ) | ||
130 | { | ||
131 | raiseWidget(thumb); | ||
132 | updateSizes(); | ||
133 | } | ||
134 | else | ||
135 | { | ||
136 | raiseWidget(detailed); | ||
137 | updateSizes(); | ||
138 | } | ||
139 | |||
140 | |||
141 | } | ||
142 | |||
143 | void ImageFileSelector::resizeEvent(QResizeEvent *) | ||
144 | { | ||
145 | updateSizes(); | ||
146 | } | ||
147 | |||
148 | void ImageFileSelector::updateSizes() | ||
149 | { | ||
150 | int ww=(detailed->width()-detailed->frameWidth()*2); | ||
151 | double w=(double)ww*0.70; | ||
152 | detailed->setColumnWidth(0,(int)w); | ||
153 | detailed->setColumnWidth(1,ww-(int)w); | ||
154 | background->setMinimumWidth(thumb->visibleWidth()); | ||
155 | thumb->updateScrollBars(); | ||
156 | } | ||
157 | |||
158 | void ImageFileSelector::reread(bool) | ||
159 | { | ||
160 | // odebug << "reread" << oendl; | ||
161 | ImageFileSelectorItem *item = (ImageFileSelectorItem *)detailed->selectedItem(); | ||
162 | QString oldFile; | ||
163 | if ( item ) | ||
164 | oldFile = item->file().file(); | ||
165 | detailed->clear(); | ||
166 | tList.clear(); | ||
167 | DocLnkSet files; | ||
168 | Global::findDocuments(&files, "image/*"); | ||
169 | count = files.children().count(); | ||
170 | QListIterator<DocLnk> dit( files.children() ); | ||
171 | // int y=0; | ||
172 | // int x=4; | ||
173 | // int totalHeight=4; | ||
174 | ThumbWidget *l=0; | ||
175 | int width=80; | ||
176 | gl->expand(dit.count()/2,2); | ||
177 | int i,j; | ||
178 | i=j=0; | ||
179 | detailed->setUpdatesEnabled(false); | ||
180 | thumb->setUpdatesEnabled(false); | ||
181 | |||
182 | for ( ; dit.current(); ++dit ) { | ||
183 | item = new ImageFileSelectorItem( detailed, **dit ); | ||
184 | if ( item->file().file() == oldFile ) | ||
185 | detailed->setCurrentItem( item ); | ||
186 | } | ||
187 | |||
188 | QListViewItemIterator it( detailed ); | ||
189 | ImageFileSelectorItem *ii; | ||
190 | // iterate through all items of the listview | ||
191 | for ( ; it.current(); ++it ) { | ||
192 | ii=(ImageFileSelectorItem *)it.current(); | ||
193 | QImage img( ii->file().file() ); | ||
194 | if( !img.isNull()) { | ||
195 | img=img.smoothScale(64,64); | ||
196 | QPixmap pix; | ||
197 | pix.convertFromImage(img); | ||
198 | l=new ThumbWidget(pix,ii->file().name(),ii->file(),background,width); | ||
199 | l->setBackgroundColor(colorGroup().base()); | ||
200 | gl->addWidget(l,j,i); | ||
201 | i++; | ||
202 | if ( i==2 ) { | ||
203 | i=0; | ||
204 | j++; | ||
205 | } | ||
206 | tList.append(l); | ||
207 | connect(l,SIGNAL(clicked(const DocLnk&)),this,SLOT(thumbClicked(const DocLnk&))); | ||
208 | } | ||
209 | } | ||
210 | |||
211 | if ( !detailed->selectedItem() ) | ||
212 | detailed->setCurrentItem( detailed->firstChild() ); | ||
213 | |||
214 | detailed->setUpdatesEnabled(true); | ||
215 | thumb->setUpdatesEnabled(true); | ||
216 | detailed->update(); | ||
217 | thumb->update(); | ||
218 | } | ||
219 | |||
220 | int ImageFileSelector::fileCount() | ||
221 | { | ||
222 | return count; | ||
223 | } | ||
224 | const DocLnk * ImageFileSelector::selected() | ||
225 | { | ||
226 | odebug << "image selected" << oendl; | ||
227 | ImageFileSelectorItem *item = (ImageFileSelectorItem *) detailed->selectedItem(); | ||
228 | if ( item ) | ||
229 | return new DocLnk( item->file() ); | ||
230 | return 0; | ||
231 | } | ||
232 | |||
233 | |||
234 | |||
235 | void ImageFileSelector::fileClicked( int button, QListViewItem *i, const QPoint &, int ) | ||
236 | { | ||
237 | if ( !i ) | ||
238 | return; | ||
239 | if ( button == Qt::LeftButton ) | ||
240 | { | ||
241 | fileClicked( i ); | ||
242 | } | ||
243 | } | ||
244 | // pressed to get 'right down' | ||
245 | void ImageFileSelector::filePressed( int, QListViewItem *, const QPoint &, int ) | ||
246 | { | ||
247 | |||
248 | } | ||
249 | void ImageFileSelector::fileClicked( QListViewItem *i) | ||
250 | { | ||
251 | if ( !i ) | ||
252 | return; | ||
253 | emit fileSelected( ( (ImageFileSelectorItem*)i )->file() ); | ||
254 | emit closeMe(); | ||
255 | } | ||
256 | |||
257 | void ImageFileSelector::thumbClicked(const DocLnk &f) | ||
258 | { | ||
259 | emit fileSelected( f ); | ||
260 | emit closeMe(); | ||
261 | } | ||
262 | |||