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 | { | 38 | { | ||
elvisangelaccio: Not needed, the constructor is implicitly called for QMap objects. | |||||
39 | connect(this, &DolphinTabWidget::tabCloseRequested, | 39 | connect(this, &DolphinTabWidget::tabCloseRequested, | ||
40 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab)); | 40 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::closeTab)); | ||
41 | connect(this, &DolphinTabWidget::currentChanged, | 41 | connect(this, &DolphinTabWidget::currentChanged, | ||
42 | this, &DolphinTabWidget::currentTabChanged); | 42 | this, &DolphinTabWidget::currentTabChanged); | ||
43 | 43 | | |||
44 | DolphinTabBar* tabBar = new DolphinTabBar(this); | 44 | DolphinTabBar* tabBar = new DolphinTabBar(this); | ||
45 | connect(tabBar, &DolphinTabBar::openNewActivatedTab, | 45 | connect(tabBar, &DolphinTabBar::openNewActivatedTab, | ||
46 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab)); | 46 | this, static_cast<void (DolphinTabWidget::*)(int)>(&DolphinTabWidget::openNewActivatedTab)); | ||
47 | connect(tabBar, &DolphinTabBar::tabDropEvent, | 47 | connect(tabBar, &DolphinTabBar::tabDropEvent, | ||
48 | this, &DolphinTabWidget::tabDropEvent); | 48 | this, &DolphinTabWidget::tabDropEvent); | ||
49 | connect(tabBar, &DolphinTabBar::tabDetachRequested, | 49 | connect(tabBar, &DolphinTabBar::tabDetachRequested, | ||
50 | this, &DolphinTabWidget::detachTab); | 50 | this, &DolphinTabWidget::detachTab); | ||
51 | connect(tabBar, &DolphinTabBar::tabRenamed, this, &DolphinTabWidget::renameTab); | ||||
51 | tabBar->hide(); | 52 | tabBar->hide(); | ||
52 | 53 | | |||
53 | setTabBar(tabBar); | 54 | setTabBar(tabBar); | ||
54 | setDocumentMode(true); | 55 | setDocumentMode(true); | ||
55 | setElideMode(Qt::ElideRight); | 56 | setElideMode(Qt::ElideRight); | ||
56 | setUsesScrollButtons(true); | 57 | setUsesScrollButtons(true); | ||
57 | } | 58 | } | ||
58 | 59 | | |||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 95 | { | |||
111 | const int index = group.readEntry("Active Tab Index", 0); | 112 | const int index = group.readEntry("Active Tab Index", 0); | ||
112 | setCurrentIndex(index); | 113 | setCurrentIndex(index); | ||
113 | } | 114 | } | ||
114 | 115 | | |||
115 | void DolphinTabWidget::refreshViews() | 116 | void DolphinTabWidget::refreshViews() | ||
116 | { | 117 | { | ||
117 | const int tabCount = count(); | 118 | const int tabCount = count(); | ||
118 | for (int i = 0; i < tabCount; ++i) { | 119 | for (int i = 0; i < tabCount; ++i) { | ||
120 | auto uuid = tabPageAt(i)->uuid(); | ||||
hallas: Maybe this logic should be moved to the `tabName` function instead? | |||||
121 | if (m_tabNames.contains(uuid)) { | ||||
122 | tabBar()->setTabText(i, m_tabNames.value(uuid)); | ||||
123 | } else { | ||||
119 | tabBar()->setTabText(i, tabName(tabPageAt(i))); | 124 | tabBar()->setTabText(i, tabName(tabPageAt(i))); | ||
125 | } | ||||
120 | tabPageAt(i)->refreshViews(); | 126 | tabPageAt(i)->refreshViews(); | ||
121 | } | 127 | } | ||
122 | } | 128 | } | ||
123 | 129 | | |||
124 | void DolphinTabWidget::openNewActivatedTab() | 130 | void DolphinTabWidget::openNewActivatedTab() | ||
125 | { | 131 | { | ||
126 | const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); | 132 | const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer(); | ||
127 | Q_ASSERT(oldActiveViewContainer); | 133 | Q_ASSERT(oldActiveViewContainer); | ||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Line(s) | 234 | if (count() < 2) { | |||
230 | parentWidget()->close(); | 236 | parentWidget()->close(); | ||
231 | return; | 237 | return; | ||
232 | } | 238 | } | ||
233 | 239 | | |||
234 | DolphinTabPage* tabPage = tabPageAt(index); | 240 | DolphinTabPage* tabPage = tabPageAt(index); | ||
235 | emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); | 241 | emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState()); | ||
236 | 242 | | |||
237 | removeTab(index); | 243 | removeTab(index); | ||
244 | m_tabNames.remove(tabPage->uuid()); | ||||
238 | tabPage->deleteLater(); | 245 | tabPage->deleteLater(); | ||
239 | } | 246 | } | ||
240 | 247 | | |||
241 | void DolphinTabWidget::activateNextTab() | 248 | void DolphinTabWidget::activateNextTab() | ||
242 | { | 249 | { | ||
243 | const int index = currentIndex() + 1; | 250 | const int index = currentIndex() + 1; | ||
244 | setCurrentIndex(index < count() ? index : 0); | 251 | setCurrentIndex(index < count() ? index : 0); | ||
245 | } | 252 | } | ||
Show All 18 Lines | |||||
264 | } | 271 | } | ||
265 | 272 | | |||
266 | void DolphinTabWidget::restoreClosedTab(const QByteArray& state) | 273 | void DolphinTabWidget::restoreClosedTab(const QByteArray& state) | ||
267 | { | 274 | { | ||
268 | openNewActivatedTab(); | 275 | openNewActivatedTab(); | ||
269 | currentTabPage()->restoreState(state); | 276 | currentTabPage()->restoreState(state); | ||
270 | } | 277 | } | ||
271 | 278 | | |||
279 | void DolphinTabWidget::renameTab(int index, const QString& name) | ||||
elvisangelaccio: Coding style: brace should go to next line for functions. | |||||
280 | { | ||||
281 | auto uuid = tabPageAt(index)->uuid(); | ||||
282 | if (name.isEmpty()) { | ||||
283 | m_tabNames.remove(uuid); | ||||
284 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | ||||
285 | } else { | ||||
elvisangelaccio: Coding style: else starts at the end of previous line. | |||||
286 | tabBar()->setTabText(index, name); | ||||
287 | m_tabNames.insert(uuid, name); | ||||
288 | } | ||||
289 | } | ||||
290 | | ||||
272 | void DolphinTabWidget::detachTab(int index) | 291 | void DolphinTabWidget::detachTab(int index) | ||
273 | { | 292 | { | ||
274 | Q_ASSERT(index >= 0); | 293 | Q_ASSERT(index >= 0); | ||
275 | 294 | | |||
276 | QStringList args; | 295 | QStringList args; | ||
277 | 296 | | |||
278 | const DolphinTabPage* tabPage = tabPageAt(index); | 297 | const DolphinTabPage* tabPage = tabPageAt(index); | ||
279 | args << tabPage->primaryViewContainer()->url().url(); | 298 | args << tabPage->primaryViewContainer()->url().url(); | ||
280 | if (tabPage->splitViewEnabled()) { | 299 | if (tabPage->splitViewEnabled()) { | ||
281 | args << tabPage->secondaryViewContainer()->url().url(); | 300 | args << tabPage->secondaryViewContainer()->url().url(); | ||
282 | args << QStringLiteral("--split"); | 301 | args << QStringLiteral("--split"); | ||
283 | } | 302 | } | ||
284 | 303 | | |||
304 | m_tabNames.remove(tabPage->uuid()); | ||||
285 | const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); | 305 | const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args)); | ||
286 | KRun::runCommand(command, this); | 306 | KRun::runCommand(command, this); | ||
287 | 307 | | |||
288 | closeTab(index); | 308 | closeTab(index); | ||
289 | } | 309 | } | ||
290 | 310 | | |||
291 | void DolphinTabWidget::openNewActivatedTab(int index) | 311 | void DolphinTabWidget::openNewActivatedTab(int index) | ||
292 | { | 312 | { | ||
293 | Q_ASSERT(index >= 0); | 313 | Q_ASSERT(index >= 0); | ||
294 | const DolphinTabPage* tabPage = tabPageAt(index); | 314 | const DolphinTabPage* tabPage = tabPageAt(index); | ||
295 | openNewActivatedTab(tabPage->activeViewContainer()->url()); | 315 | openNewActivatedTab(tabPage->activeViewContainer()->url()); | ||
296 | } | 316 | } | ||
297 | 317 | | |||
298 | void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) | 318 | void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) | ||
299 | { | 319 | { | ||
300 | if (index >= 0) { | 320 | if (index >= 0) { | ||
301 | DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); | 321 | DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); | ||
302 | view->dropUrls(view->url(), event, view); | 322 | view->dropUrls(view->url(), event, view); | ||
303 | } | 323 | } | ||
304 | } | 324 | } | ||
305 | 325 | | |||
306 | void DolphinTabWidget::tabUrlChanged(const QUrl& url) | 326 | void DolphinTabWidget::tabUrlChanged(const QUrl& url) | ||
307 | { | 327 | { | ||
308 | const int index = indexOf(qobject_cast<QWidget*>(sender())); | 328 | const int index = indexOf(qobject_cast<QWidget*>(sender())); | ||
329 | auto uuid = tabPageAt(index)->uuid(); | ||||
309 | if (index >= 0) { | 330 | if (index >= 0) { | ||
331 | if (!m_tabNames.contains(uuid)) { | ||||
310 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | 332 | tabBar()->setTabText(index, tabName(tabPageAt(index))); | ||
333 | } | ||||
311 | tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); | 334 | tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url))); | ||
312 | 335 | | |||
313 | // Emit the currentUrlChanged signal if the url of the current tab has been changed. | 336 | // Emit the currentUrlChanged signal if the url of the current tab has been changed. | ||
314 | if (index == currentIndex()) { | 337 | if (index == currentIndex()) { | ||
315 | emit currentUrlChanged(url); | 338 | emit currentUrlChanged(url); | ||
316 | } | 339 | } | ||
317 | } | 340 | } | ||
318 | } | 341 | } | ||
▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines |
Don't want this in production code