Changeset View
Changeset View
Standalone View
Standalone View
krusader/Panel/PanelView/krinterview.cpp
Show First 20 Lines • Show All 125 Lines • ▼ Show 20 Line(s) | 118 | { | |||
---|---|---|---|---|---|
126 | return getKrViewItem(ndx); | 126 | return getKrViewItem(ndx); | ||
127 | } | 127 | } | ||
128 | 128 | | |||
129 | QString KrInterView::getCurrentItem() const | 129 | QString KrInterView::getCurrentItem() const | ||
130 | { | 130 | { | ||
131 | if (!_model->ready()) | 131 | if (!_model->ready()) | ||
132 | return QString(); | 132 | return QString(); | ||
133 | 133 | | |||
134 | FileItem *fileitem = _model->fileItemAt(_itemView->currentIndex()); | 134 | FileItem *fileItem = _model->fileItemAt(_itemView->currentIndex()); | ||
135 | if (fileitem == 0) | 135 | return fileItem ? fileItem->getName() : QString(); | ||
136 | return QString(); | | |||
137 | return fileitem->getName(); | | |||
138 | } | 136 | } | ||
139 | 137 | | |||
140 | KrViewItem* KrInterView::getCurrentKrViewItem() | 138 | KrViewItem* KrInterView::getCurrentKrViewItem() | ||
141 | { | 139 | { | ||
142 | if (!_model->ready()) | 140 | if (!_model->ready()) | ||
143 | return 0; | 141 | return 0; | ||
144 | 142 | | |||
145 | return getKrViewItem(_itemView->currentIndex()); | 143 | return getKrViewItem(_itemView->currentIndex()); | ||
Show All 36 Lines | |||||
182 | KrViewItem* KrInterView::getKrViewItemAt(const QPoint &vp) | 180 | KrViewItem* KrInterView::getKrViewItemAt(const QPoint &vp) | ||
183 | { | 181 | { | ||
184 | if (!_model->ready()) | 182 | if (!_model->ready()) | ||
185 | return 0; | 183 | return 0; | ||
186 | 184 | | |||
187 | return getKrViewItem(_itemView->indexAt(vp)); | 185 | return getKrViewItem(_itemView->indexAt(vp)); | ||
188 | } | 186 | } | ||
189 | 187 | | |||
190 | KrViewItem *KrInterView::findItemByFileItem(FileItem *fileItem) { | | |||
191 | return getKrViewItem(fileItem); | | |||
192 | } | | |||
193 | | ||||
194 | KrViewItem * KrInterView::getKrViewItem(FileItem *fileItem) | 188 | KrViewItem * KrInterView::getKrViewItem(FileItem *fileItem) | ||
195 | { | 189 | { | ||
196 | QHash<FileItem *, KrViewItem*>::iterator it = _itemHash.find(fileItem); | 190 | QHash<FileItem *, KrViewItem*>::iterator it = _itemHash.find(fileItem); | ||
197 | if (it == _itemHash.end()) { | 191 | if (it == _itemHash.end()) { | ||
198 | KrViewItem * newItem = new KrViewItem(fileItem, this); | 192 | KrViewItem * newItem = new KrViewItem(fileItem, this); | ||
199 | _itemHash[ fileItem ] = newItem; | 193 | _itemHash[ fileItem ] = newItem; | ||
200 | return newItem; | 194 | return newItem; | ||
201 | } | 195 | } | ||
202 | return *it; | 196 | return *it; | ||
203 | } | 197 | } | ||
204 | 198 | | |||
205 | KrViewItem * KrInterView::getKrViewItem(const QModelIndex & ndx) | 199 | KrViewItem * KrInterView::getKrViewItem(const QModelIndex & ndx) | ||
206 | { | 200 | { | ||
207 | if (!ndx.isValid()) | 201 | if (!ndx.isValid()) | ||
208 | return 0; | 202 | return 0; | ||
209 | FileItem * fileitem = _model->fileItemAt(ndx); | 203 | FileItem * fileitem = _model->fileItemAt(ndx); | ||
210 | if (fileitem == 0) | 204 | if (fileitem == 0) | ||
211 | return 0; | 205 | return 0; | ||
212 | else | 206 | else | ||
213 | return getKrViewItem(fileitem); | 207 | return getKrViewItem(fileitem); | ||
214 | } | 208 | } | ||
215 | 209 | | |||
216 | void KrInterView::makeCurrentVisible() | 210 | void KrInterView::makeCurrentVisible() | ||
217 | { | 211 | { | ||
218 | qDebug() << "scroll to current index=" << _itemView->currentIndex(); | 212 | makeItemVisible(getCurrentKrViewItem()); | ||
219 | _itemView->scrollTo(_itemView->currentIndex()); | | |||
220 | } | 213 | } | ||
221 | 214 | | |||
222 | void KrInterView::makeItemVisible(const KrViewItem *item) | 215 | void KrInterView::makeItemVisible(const KrViewItem *item) | ||
223 | { | 216 | { | ||
224 | if (item == 0) | 217 | if (item == 0) | ||
225 | return; | 218 | return; | ||
226 | 219 | | |||
227 | FileItem* fileitem = (FileItem *)item->getFileItem(); | 220 | FileItem* fileitem = (FileItem *)item->getFileItem(); | ||
228 | QModelIndex ndx = _model->fileItemIndex(fileitem); | 221 | const QModelIndex index = _model->fileItemIndex(fileitem); | ||
229 | qDebug() << "scroll to item name=" << fileitem->getName(); | 222 | qDebug() << "scroll to item; name=" << fileitem->getName() << " index=" << index; | ||
230 | if (ndx.isValid()) | 223 | if (index.isValid()) | ||
231 | _itemView->scrollTo(ndx); | 224 | _itemView->scrollTo(index); | ||
232 | } | 225 | } | ||
233 | 226 | | |||
234 | bool KrInterView::isItemVisible(const KrViewItem *item) | 227 | bool KrInterView::isItemVisible(const KrViewItem *item) | ||
235 | { | 228 | { | ||
236 | return _itemView->viewport()->rect().contains(item->itemRect()); | 229 | return item && _itemView->viewport()->rect().contains(item->itemRect()); | ||
237 | } | 230 | } | ||
238 | 231 | | |||
239 | void KrInterView::setCurrentItem(const QString& name, const QModelIndex &fallbackToIndex) | 232 | void KrInterView::setCurrentItem(const QString &name, bool scrollToCurrent, | ||
233 | const QModelIndex &fallbackToIndex) | ||||
240 | { | 234 | { | ||
241 | // find index by given name and set it as current | 235 | // find index by given name and set it as current | ||
242 | QModelIndex ndx = _model->nameIndex(name); | 236 | const QModelIndex index = _model->nameIndex(name); | ||
243 | if (ndx.isValid()) { | 237 | if (index.isValid()) { | ||
244 | // also sets the scrolling position | 238 | setCurrent(index, scrollToCurrent); | ||
245 | _itemView->setCurrentIndex(ndx); | | |||
246 | } else if (fallbackToIndex.isValid()) { | 239 | } else if (fallbackToIndex.isValid()) { | ||
247 | // set fallback index as current index | 240 | // set fallback index as current if not too big, else set the last item as current | ||
248 | // when fallback index is too big, set the last item as current | 241 | if (fallbackToIndex.row() < _itemView->model()->rowCount()) { | ||
249 | if (fallbackToIndex.row() >= _itemView->model()->rowCount()) { | 242 | setCurrent(fallbackToIndex, scrollToCurrent); | ||
250 | setCurrentKrViewItem(getLast()); | | |||
251 | } else { | 243 | } else { | ||
252 | _itemView->setCurrentIndex(fallbackToIndex); | 244 | setCurrentKrViewItem(getLast(), scrollToCurrent); | ||
253 | } | 245 | } | ||
254 | } else { | 246 | } else { | ||
255 | // when given parameters fail, set the first item as current | 247 | // when given parameters fail, set the first item as current | ||
256 | setCurrentKrViewItem(getFirst()); | 248 | setCurrentKrViewItem(getFirst(), scrollToCurrent); | ||
257 | } | 249 | } | ||
258 | } | 250 | } | ||
259 | 251 | | |||
260 | void KrInterView::setCurrentKrViewItem(KrViewItem *item) | 252 | void KrInterView::setCurrentKrViewItem(KrViewItem *item, bool scrollToCurrent) | ||
261 | { | 253 | { | ||
262 | if (item == 0) { | 254 | if (!item) { | ||
263 | _itemView->setCurrentIndex(QModelIndex()); | 255 | setCurrent(QModelIndex(), scrollToCurrent); | ||
264 | return; | 256 | return; | ||
265 | } | 257 | } | ||
258 | | ||||
266 | FileItem* fileitem = (FileItem *)item->getFileItem(); | 259 | FileItem* fileitem = (FileItem *)item->getFileItem(); | ||
267 | QModelIndex ndx = _model->fileItemIndex(fileitem); | 260 | const QModelIndex index = _model->fileItemIndex(fileitem); | ||
268 | if (ndx.isValid() && ndx.row() != _itemView->currentIndex().row()) { | 261 | if (index.isValid() && index.row() != _itemView->currentIndex().row()) { | ||
262 | setCurrent(index, scrollToCurrent); | ||||
263 | } | ||||
264 | } | ||||
265 | | ||||
266 | void KrInterView::setCurrent(const QModelIndex &index, bool scrollToCurrent) { | ||||
267 | const bool disableAutoScroll = _itemView->hasAutoScroll() && !scrollToCurrent; | ||||
268 | if (disableAutoScroll) { | ||||
269 | // setCurrentIndex() scrolls to current if autoScroll is turned on | ||||
270 | _itemView->setAutoScroll(false); | ||||
271 | } | ||||
272 | | ||||
269 | _mouseHandler->cancelTwoClickRename(); | 273 | _mouseHandler->cancelTwoClickRename(); | ||
270 | _itemView->setCurrentIndex(ndx); | 274 | _itemView->setCurrentIndex(index); | ||
275 | | ||||
276 | if (disableAutoScroll) { | ||||
277 | _itemView->setAutoScroll(true); | ||||
271 | } | 278 | } | ||
272 | } | 279 | } | ||
273 | 280 | | |||
274 | void KrInterView::sort() | 281 | void KrInterView::sort() | ||
275 | { | 282 | { | ||
276 | _model->sort(); | 283 | _model->sort(); | ||
277 | } | 284 | } | ||
278 | 285 | | |||
Show All 13 Lines | |||||
292 | 299 | | |||
293 | void KrInterView::populate(const QList<FileItem*> &fileItems, FileItem *dummy) | 300 | void KrInterView::populate(const QList<FileItem*> &fileItems, FileItem *dummy) | ||
294 | { | 301 | { | ||
295 | _model->populate(fileItems, dummy); | 302 | _model->populate(fileItems, dummy); | ||
296 | } | 303 | } | ||
297 | 304 | | |||
298 | KrViewItem* KrInterView::preAddItem(FileItem *fileitem) | 305 | KrViewItem* KrInterView::preAddItem(FileItem *fileitem) | ||
299 | { | 306 | { | ||
300 | QModelIndex idx = _model->addItem(fileitem); | 307 | const QModelIndex index = _model->addItem(fileitem); | ||
301 | if(_model->rowCount() == 1) // if this is the fist item to be added, make it current | 308 | return getKrViewItem(index); | ||
302 | _itemView->setCurrentIndex(idx); | | |||
303 | return getKrViewItem(idx); | | |||
304 | } | 309 | } | ||
305 | 310 | | |||
306 | void KrInterView::preDelItem(KrViewItem *item) | 311 | void KrInterView::preDeleteItem(KrViewItem *item) | ||
307 | { | 312 | { | ||
308 | setSelected(item->getFileItem(), false); | 313 | setSelected(item->getFileItem(), false); | ||
309 | QModelIndex ndx = _model->removeItem((FileItem *)item->getFileItem()); | 314 | | ||
310 | if (ndx.isValid()) | 315 | // if the next item is current, current selection is lost on remove; preserve manually | ||
311 | _itemView->setCurrentIndex(ndx); | 316 | KrViewItem *currentItem = getCurrentKrViewItem(); | ||
317 | KrViewItem *nextCurrentItem = currentItem && currentItem == getNext(item) ? currentItem : 0; | ||||
318 | | ||||
319 | _model->removeItem((FileItem *)item->getFileItem()); | ||||
320 | | ||||
321 | if (nextCurrentItem) | ||||
322 | setCurrentKrViewItem(nextCurrentItem, false); | ||||
323 | | ||||
312 | _itemHash.remove((FileItem *)item->getFileItem()); | 324 | _itemHash.remove((FileItem *)item->getFileItem()); | ||
313 | } | 325 | } | ||
314 | 326 | | |||
315 | void KrInterView::prepareForActive() | 327 | void KrInterView::prepareForActive() | ||
316 | { | 328 | { | ||
317 | _focused = true; | 329 | _focused = true; | ||
318 | _itemView->setFocus(); | 330 | _itemView->setFocus(); | ||
319 | } | 331 | } | ||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |