Changeset View
Changeset View
Standalone View
Standalone View
src/dolphintabwidget.cpp
Show All 24 Lines | |||||
25 | 25 | | |||
26 | #include <KConfigGroup> | 26 | #include <KConfigGroup> | ||
27 | #include <KRun> | 27 | #include <KRun> | ||
28 | #include <KShell> | 28 | #include <KShell> | ||
29 | #include <kio/global.h> | 29 | #include <kio/global.h> | ||
30 | 30 | | |||
31 | #include <QApplication> | 31 | #include <QApplication> | ||
32 | #include <QDropEvent> | 32 | #include <QDropEvent> | ||
33 | 33 | | |||
ngraham: Don't want this in production code | |||||
34 | DolphinTabWidget::DolphinTabWidget(QWidget* parent) : | 34 | DolphinTabWidget::DolphinTabWidget(QWidget* parent) : | ||
35 | QTabWidget(parent), | 35 | QTabWidget(parent), | ||
36 | m_placesSelectorVisible(true), | 36 | m_placesSelectorVisible(true), | ||
37 | m_lastViewedTab(0) | 37 | m_lastViewedTab(0), | ||
38 | m_tabNames() | ||||
elvisangelaccio: Not needed, the constructor is implicitly called for QMap objects. | |||||
38 | { | 39 | { | ||
39 | connect(this, &DolphinTabWidget::tabCloseRequested, | 40 | connect(this, &DolphinTabWidget::tabCloseRequested, | ||
40 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab)); | 41 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab)); | ||
41 | connect(this, &DolphinTabWidget::currentChanged, | 42 | connect(this, &DolphinTabWidget::currentChanged, | ||
42 | this, &DolphinTabWidget::currentTabChanged); | 43 | this, &DolphinTabWidget::currentTabChanged); | ||
43 | 44 | | |||
44 | DolphinTabBar* tabBar = new DolphinTabBar(this); | 45 | DolphinTabBar* tabBar = new DolphinTabBar(this); | ||
45 | connect(tabBar, &DolphinTabBar::openNewActivatedTab, | 46 | connect(tabBar, &DolphinTabBar::openNewActivatedTab, | ||
46 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab)); | 47 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab)); | ||
47 | connect(tabBar, &DolphinTabBar::tabDropEvent, | 48 | connect(tabBar, &DolphinTabBar::tabDropEvent, | ||
48 | this, &DolphinTabWidget::tabDropEvent); | 49 | this, &DolphinTabWidget::tabDropEvent); | ||
49 | connect(tabBar, &DolphinTabBar::tabDetachRequested, | 50 | connect(tabBar, &DolphinTabBar::tabDetachRequested, | ||
50 | this, &DolphinTabWidget::detachTab); | 51 | this, &DolphinTabWidget::detachTab); | ||
52 | connect(tabBar, &DolphinTabBar::tabRenamed, this, &DolphinTabWidget::renameTab); | ||||
51 | tabBar->hide(); | 53 | tabBar->hide(); | ||
52 | 54 | | |||
53 | setTabBar(tabBar); | 55 | setTabBar(tabBar); | ||
54 | setDocumentMode(true); | 56 | setDocumentMode(true); | ||
55 | setElideMode(Qt::ElideRight); | 57 | setElideMode(Qt::ElideRight); | ||
56 | setUsesScrollButtons(true); | 58 | setUsesScrollButtons(true); | ||
57 | } | 59 | } | ||
58 | 60 | | |||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 96 | { | |||
111 | const int index = group.readEntry("Active Tab Index", 0); | 113 | const int index = group.readEntry("Active Tab Index", 0); | ||
112 | setCurrentIndex(index); | 114 | setCurrentIndex(index); | ||
113 | } | 115 | } | ||
114 | 116 | | |||
115 | void DolphinTabWidget::refreshViews() | 117 | void DolphinTabWidget::refreshViews() | ||
116 | { | 118 | { | ||
117 | const int tabCount = count(); | 119 | const int tabCount = count(); | ||
118 | for (int i = 0; i < tabCount; ++i) { | 120 | for (int i = 0; i < tabCount; ++i) { | ||
121 | auto uuid = tabPageAt(i)->getUuid(); | ||||
122 | if (m_tabNames.contains(uuid)) { | ||||
123 | tabBar()->setTabText(i, m_tabNames.value(uuid)); | ||||
124 | } else { | ||||
119 | tabBar()->setTabText(i, tabName(tabPageAt(i))); | 125 | tabBar()->setTabText(i, tabName(tabPageAt(i))); | ||
126 | } | ||||
hallas: Maybe this logic should be moved to the `tabName` function instead? | |||||
120 | tabPageAt(i)->refreshViews(); | 127 | tabPageAt(i)->refreshViews(); | ||
121 | } | 128 | } | ||
122 | } | 129 | } | ||
123 | 130 | | |||
124 | void DolphinTabWidget::openNewActivatedTab() | 131 | void DolphinTabWidget::openNewActivatedTab() | ||
125 | { | 132 | { | ||
126 | const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); | 133 | const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); | ||
127 | Q_ASSERT(oldActiveViewContainer); | 134 | Q_ASSERT(oldActiveViewContainer); | ||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Line(s) | 235 | if (count() < 2) { | |||
230 | parentWidget()->close(); | 237 | parentWidget()->close(); | ||
231 | return; | 238 | return; | ||
232 | } | 239 | } | ||
233 | 240 | | |||
234 | DolphinTabPage* tabPage = tabPageAt(index); | 241 | DolphinTabPage* tabPage = tabPageAt(index); | ||
235 | emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); | 242 | emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); | ||
236 | 243 | | |||
237 | removeTab(index); | 244 | removeTab(index); | ||
245 | m_tabNames.remove(tabPage->getUuid()); | ||||
238 | tabPage->deleteLater(); | 246 | tabPage->deleteLater(); | ||
239 | } | 247 | } | ||
240 | 248 | | |||
241 | void DolphinTabWidget::activateNextTab() | 249 | void DolphinTabWidget::activateNextTab() | ||
242 | { | 250 | { | ||
243 | const int index = currentIndex() + 1; | 251 | const int index = currentIndex() + 1; | ||
244 | setCurrentIndex(index < count() ? index : 0); | 252 | setCurrentIndex(index < count() ? index : 0); | ||
245 | } | 253 | } | ||
Show All 18 Lines | |||||
264 | } | 272 | } | ||
265 | 273 | | |||
266 | void DolphinTabWidget::restoreClosedTab(const QByteArray& state) | 274 | void DolphinTabWidget::restoreClosedTab(const QByteArray& state) | ||
267 | { | 275 | { | ||
268 | openNewActivatedTab(); | 276 | openNewActivatedTab(); | ||
269 | currentTabPage()->restoreState(state); | 277 | currentTabPage()->restoreState(state); | ||
270 | } | 278 | } | ||
271 | 279 | | |||
280 | void DolphinTabWidget::renameTab(int index, QString& name) { | ||||
elvisangelaccio: Coding style: brace should go to next line for functions. | |||||
281 | auto uuid = tabPageAt(index)->getUuid(); | ||||
282 | if (name.isEmpty()) { | ||||
283 | m_tabNames.remove(uuid); | ||||
284 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | ||||
285 | } | ||||
286 | else { | ||||
elvisangelaccio: Coding style: else starts at the end of previous line. | |||||
287 | tabBar()->setTabText(index, name); | ||||
288 | m_tabNames.insert(uuid, name); | ||||
289 | } | ||||
290 | } | ||||
291 | | ||||
272 | void DolphinTabWidget::detachTab(int index) | 292 | void DolphinTabWidget::detachTab(int index) | ||
273 | { | 293 | { | ||
274 | Q_ASSERT(index >= 0); | 294 | Q_ASSERT(index >= 0); | ||
275 | 295 | | |||
276 | QStringList args; | 296 | QStringList args; | ||
277 | 297 | auto uuid = tabPageAt(index)->getUuid(); | |||
278 | const DolphinTabPage* tabPage = tabPageAt(index); | 298 | const DolphinTabPage* tabPage = tabPageAt(index); | ||
299 | uuid = tabPage->getUuid(); | ||||
279 | args << tabPage->primaryViewContainer()->url().url(); | 300 | args << tabPage->primaryViewContainer()->url().url(); | ||
280 | if (tabPage->splitViewEnabled()) { | 301 | if (tabPage->splitViewEnabled()) { | ||
281 | args << tabPage->secondaryViewContainer()->url().url(); | 302 | args << tabPage->secondaryViewContainer()->url().url(); | ||
282 | args << QStringLiteral("--split"); | 303 | args << QStringLiteral("--split"); | ||
283 | } | 304 | } | ||
284 | 305 | m_tabNames.remove(uuid); | |||
285 | const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); | 306 | const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); | ||
286 | KRun::runCommand(command, this); | 307 | KRun::runCommand(command, this); | ||
287 | 308 | | |||
288 | closeTab(index); | 309 | closeTab(index); | ||
289 | } | 310 | } | ||
290 | 311 | | |||
291 | void DolphinTabWidget::openNewActivatedTab(int index) | 312 | void DolphinTabWidget::openNewActivatedTab(int index) | ||
292 | { | 313 | { | ||
293 | Q_ASSERT(index >= 0); | 314 | Q_ASSERT(index >= 0); | ||
294 | const DolphinTabPage* tabPage = tabPageAt(index); | 315 | const DolphinTabPage* tabPage = tabPageAt(index); | ||
295 | openNewActivatedTab(tabPage->activeViewContainer()->url()); | 316 | openNewActivatedTab(tabPage->activeViewContainer()->url()); | ||
296 | } | 317 | } | ||
297 | 318 | | |||
298 | void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) | 319 | void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) | ||
299 | { | 320 | { | ||
300 | if (index >= 0) { | 321 | if (index >= 0) { | ||
301 | DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); | 322 | DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); | ||
302 | view->dropUrls(view->url(), event, view); | 323 | view->dropUrls(view->url(), event, view); | ||
303 | } | 324 | } | ||
304 | } | 325 | } | ||
305 | 326 | | |||
306 | void DolphinTabWidget::tabUrlChanged(const QUrl& url) | 327 | void DolphinTabWidget::tabUrlChanged(const QUrl& url) | ||
307 | { | 328 | { | ||
308 | const int index = indexOf(qobject_cast<QWidget*>(sender())); | 329 | const int index = indexOf(qobject_cast<QWidget*>(sender())); | ||
330 | auto uuid = tabPageAt(index)->getUuid(); | ||||
309 | if (index >= 0) { | 331 | if (index >= 0) { | ||
332 | if (!m_tabNames.contains(uuid)) { | ||||
310 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | 333 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | ||
334 | } | ||||
311 | tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); | 335 | tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); | ||
312 | 336 | | |||
313 | // Emit the currentUrlChanged signal if the url of the current tab has been changed. | 337 | // Emit the currentUrlChanged signal if the url of the current tab has been changed. | ||
314 | if (index == currentIndex()) { | 338 | if (index == currentIndex()) { | ||
315 | emit currentUrlChanged(url); | 339 | emit currentUrlChanged(url); | ||
316 | } | 340 | } | ||
317 | } | 341 | } | ||
318 | } | 342 | } | ||
Show All 10 Lines | 345 | { | |||
329 | emit currentUrlChanged(viewContainer->url()); | 353 | emit currentUrlChanged(viewContainer->url()); | ||
330 | tabPage->setActive(true); | 354 | tabPage->setActive(true); | ||
331 | m_lastViewedTab = index; | 355 | m_lastViewedTab = index; | ||
332 | } | 356 | } | ||
333 | 357 | | |||
334 | void DolphinTabWidget::tabInserted(int index) | 358 | void DolphinTabWidget::tabInserted(int index) | ||
335 | { | 359 | { | ||
336 | QTabWidget::tabInserted(index); | 360 | QTabWidget::tabInserted(index); | ||
337 | | ||||
338 | if (count() > 1) { | 361 | if (count() > 1) { | ||
339 | tabBar()->show(); | 362 | tabBar()->show(); | ||
340 | } | 363 | } | ||
341 | 364 | | |||
342 | emit tabCountChanged(count()); | 365 | emit tabCountChanged(count()); | ||
343 | } | 366 | } | ||
344 | 367 | | |||
345 | void DolphinTabWidget::tabRemoved(int index) | 368 | void DolphinTabWidget::tabRemoved(int index) | ||
346 | { | 369 | { | ||
347 | QTabWidget::tabRemoved(index); | 370 | QTabWidget::tabRemoved(index); | ||
348 | | ||||
349 | // If only one tab is left, then remove the tab entry so that | 371 | // If only one tab is left, then remove the tab entry so that | ||
350 | // closing the last tab is not possible. | 372 | // closing the last tab is not possible. | ||
351 | if (count() < 2) { | 373 | if (count() < 2) { | ||
352 | tabBar()->hide(); | 374 | tabBar()->hide(); | ||
353 | } | 375 | } | ||
354 | 376 | | |||
355 | emit tabCountChanged(count()); | 377 | emit tabCountChanged(count()); | ||
356 | } | 378 | } | ||
Show All 11 Lines |
Don't want this in production code