Changeset View
Changeset View
Standalone View
Standalone View
krusader/paneltabbar.cpp
| Show All 34 Lines | |||||
| 35 | #include <QAction> | 35 | #include <QAction> | ||
| 36 | #include <QMenu> | 36 | #include <QMenu> | ||
| 37 | 37 | | |||
| 38 | #include <KConfigCore/KSharedConfig> | 38 | #include <KConfigCore/KSharedConfig> | ||
| 39 | #include <KI18n/KLocalizedString> | 39 | #include <KI18n/KLocalizedString> | ||
| 40 | #include <KIconThemes/KIconLoader> | 40 | #include <KIconThemes/KIconLoader> | ||
| 41 | #include <KWidgetsAddons/KActionMenu> | 41 | #include <KWidgetsAddons/KActionMenu> | ||
| 42 | 42 | | |||
| 43 | #define DISPLAY(X) (X.isLocalFile() ? X.path() : X.toDisplayString()) | | |||
| 44 | | ||||
| 45 | static const int sDragEnterDelay = 500; // msec | 43 | static const int sDragEnterDelay = 500; // msec | ||
| 46 | 44 | | |||
| 47 | PanelTabBar::PanelTabBar(QWidget *parent, TabActions *actions): QTabBar(parent), | 45 | PanelTabBar::PanelTabBar(QWidget *parent, TabActions *actions): QTabBar(parent), | ||
| 48 | _maxTabLength(0), _tabClicked(false), _draggingTab(false), _dragTabIndex(-1) | 46 | _maxTabLength(0), _tabClicked(false), _draggingTab(false), _dragTabIndex(-1) | ||
| 49 | { | 47 | { | ||
| 50 | _panelActionMenu = new KActionMenu(i18n("Panel"), this); | 48 | _panelActionMenu = new KActionMenu(i18n("Panel"), this); | ||
| 51 | 49 | | |||
| 52 | setAcceptDrops(true); | 50 | setAcceptDrops(true); | ||
| Show All 33 Lines | 83 | if (nextTo) { | |||
| 86 | for (int i = 0; i < count(); i++) { | 84 | for (int i = 0; i < count(); i++) { | ||
| 87 | if (getPanel(i) == nextTo) { | 85 | if (getPanel(i) == nextTo) { | ||
| 88 | insertIndex = i + 1; | 86 | insertIndex = i + 1; | ||
| 89 | break; | 87 | break; | ||
| 90 | } | 88 | } | ||
| 91 | } | 89 | } | ||
| 92 | } | 90 | } | ||
| 93 | 91 | | |||
| 94 | const QString text = squeeze(DISPLAY(panel->virtualPath())); | 92 | const QString text = squeeze(panel->virtualPath()); | ||
| 95 | const int index = insertIndex != -1 ? insertTab(insertIndex, text) : addTab(text); | 93 | const int index = insertIndex != -1 ? insertTab(insertIndex, text) : addTab(text); | ||
| 96 | 94 | | |||
| 97 | QVariant v; | 95 | QVariant v; | ||
| 98 | v.setValue((long long)panel); | 96 | v.setValue((long long)panel); | ||
| 99 | setTabData(index, v); | 97 | setTabData(index, v); | ||
| 100 | 98 | | |||
| 101 | setIcon(index, panel); | 99 | setIcon(index, panel); | ||
| 102 | 100 | | |||
| Show All 36 Lines | 136 | { | |||
| 139 | return removePanel(currentIndex(), panelToDelete); | 137 | return removePanel(currentIndex(), panelToDelete); | ||
| 140 | } | 138 | } | ||
| 141 | 139 | | |||
| 142 | void PanelTabBar::updateTab(ListPanel *panel) | 140 | void PanelTabBar::updateTab(ListPanel *panel) | ||
| 143 | { | 141 | { | ||
| 144 | // find which is the correct tab | 142 | // find which is the correct tab | ||
| 145 | for (int i = 0; i < count(); i++) { | 143 | for (int i = 0; i < count(); i++) { | ||
| 146 | if ((ListPanel*)tabData(i).toLongLong() == panel) { | 144 | if ((ListPanel*)tabData(i).toLongLong() == panel) { | ||
| 147 | setTabText(i, squeeze(DISPLAY(panel->virtualPath()), i)); | 145 | setTabText(i, squeeze(panel->virtualPath(), i)); | ||
| 148 | setIcon(i, panel); | 146 | setIcon(i, panel); | ||
| 149 | break; | 147 | break; | ||
| 150 | } | 148 | } | ||
| 151 | } | 149 | } | ||
| 152 | } | 150 | } | ||
| 153 | 151 | | |||
| 154 | void PanelTabBar::duplicateTab() | 152 | void PanelTabBar::duplicateTab() | ||
| 155 | { | 153 | { | ||
| 156 | int id = currentIndex(); | 154 | int id = currentIndex(); | ||
| 157 | emit newTab(((ListPanel*)tabData(id).toLongLong())->virtualPath()); | 155 | emit newTab(((ListPanel*)tabData(id).toLongLong())->virtualPath()); | ||
| 158 | } | 156 | } | ||
| 159 | 157 | | |||
| 160 | void PanelTabBar::setIcon(int index, ListPanel *panel) | 158 | void PanelTabBar::setIcon(int index, ListPanel *panel) | ||
| 161 | { | 159 | { | ||
| 162 | setTabIcon(index, | 160 | setTabIcon(index, | ||
| 163 | panel->isLocked() ? krLoader->loadIcon("lock", KIconLoader::Toolbar, 16) : QIcon()); | 161 | panel->isLocked() ? krLoader->loadIcon("lock", KIconLoader::Toolbar, 16) : QIcon()); | ||
| 164 | } | 162 | } | ||
| 165 | 163 | | |||
| 166 | QString PanelTabBar::squeeze(QString text, int index) | 164 | QString PanelTabBar::squeeze(const QUrl &url, int tabIndex) | ||
| 167 | { | 165 | { | ||
| 168 | QString originalText = text; | 166 | const QString longText = url.isEmpty() ? i18n("[invalid]") : | ||
| 169 | 167 | url.isLocalFile() ? url.path() : url.toDisplayString(); | |||
| 170 | KConfigGroup group(krConfig, "Look&Feel"); | 168 | if (tabIndex >= 0) | ||
| 171 | bool longNames = group.readEntry("Fullpath Tab Names", _FullPathTabNames); | 169 | setTabToolTip(tabIndex, longText); | ||
| 172 | 170 | | |||
| 173 | if (!longNames) { | 171 | const KConfigGroup group(krConfig, "Look&Feel"); | ||
| 174 | while (text.endsWith('/')) | 172 | const bool showLongNames = group.readEntry("Fullpath Tab Names", _FullPathTabNames); | ||
| 175 | text.truncate(text.length() - 1); | 173 | | ||
| 176 | if (text.isEmpty() || text.endsWith(':')) | 174 | QString text; | ||
| 177 | text += '/'; | 175 | if (!showLongNames) { | ||
| 178 | else { | 176 | const QString scheme = url.scheme().isEmpty() || url.isLocalFile() ? "" : (url.scheme() + ":"); | ||
| 179 | QString shortName; | 177 | const QString host = url.host().isEmpty() ? "" : (url.host() + ":"); | ||
| 180 | 178 | const QString name = url.isLocalFile() && url.fileName().isEmpty() ? "/" : url.fileName(); | |||
| 181 | if (text.contains(":/")) | 179 | text = scheme + host + name; | ||
| 182 | shortName = text.left(text.indexOf(":/")) + ':'; | 180 | } else { | ||
| 183 | 181 | text = longText; | |||
| 184 | shortName += text.mid(text.lastIndexOf("/") + 1); | | |||
| 185 | text = shortName; | | |||
| 186 | } | | |||
| 187 | | ||||
| 188 | if (index >= 0) | | |||
| 189 | setTabToolTip(index, originalText); | | |||
| 190 | | ||||
| 191 | index = -1; | | |||
| 192 | } | 182 | } | ||
| 193 | 183 | | |||
| 194 | QFontMetrics fm(fontMetrics()); | 184 | if (text.isEmpty()) | ||
| 185 | text = i18nc("invalid URL path", "?"); | ||||
| 195 | 186 | | |||
| 196 | // set the real max length | 187 | // set the real max length | ||
| 188 | QFontMetrics fm(fontMetrics()); | ||||
| 197 | _maxTabLength = (static_cast<QWidget*>(parent())->width() - (6 * fm.width("W"))) / fm.width("W"); | 189 | _maxTabLength = (static_cast<QWidget*>(parent())->width() - (6 * fm.width("W"))) / fm.width("W"); | ||
| 198 | // each tab gets a fair share of the max tab length | 190 | // each tab gets a fair share of the max tab length | ||
| 199 | int _effectiveTabLength = _maxTabLength / (count() == 0 ? 1 : count()); | 191 | const int effectiveTabLength = _maxTabLength / (count() == 0 ? 1 : count()); | ||
| 192 | const int labelWidth = fm.width("W") * effectiveTabLength; | ||||
| 193 | const int textWidth = fm.width(text); | ||||
| 194 | if (textWidth <= labelWidth) | ||||
| 195 | return text; | ||||
| 200 | 196 | | |||
| 201 | int labelWidth = fm.width("W") * _effectiveTabLength; | 197 | // squeeze text - start with the dots only | ||
| 202 | int textWidth = fm.width(text); | | |||
| 203 | if (textWidth > labelWidth) { | | |||
| 204 | // start with the dots only | | |||
| 205 | QString squeezedText = "..."; | 198 | QString squeezedText = "..."; | ||
| 206 | int squeezedWidth = fm.width(squeezedText); | 199 | int squeezedWidth = fm.width(squeezedText); | ||
| 207 | 200 | | |||
| 208 | // estimate how many letters we can add to the dots on both sides | | |||
| 209 | int letters = text.length() * (labelWidth - squeezedWidth) / textWidth / 2; | 201 | int letters = text.length() * (labelWidth - squeezedWidth) / textWidth / 2; | ||
| 210 | if (labelWidth < squeezedWidth) letters = 1; | 202 | if (labelWidth < squeezedWidth) | ||
| 203 | letters = 1; | ||||
| 211 | squeezedText = text.left(letters) + "..." + text.right(letters); | 204 | squeezedText = text.left(letters) + "..." + text.right(letters); | ||
| 212 | squeezedWidth = fm.width(squeezedText); | 205 | squeezedWidth = fm.width(squeezedText); | ||
| 213 | 206 | | |||
| 214 | if (squeezedWidth < labelWidth) { | 207 | if (squeezedWidth < labelWidth) { | ||
| 215 | // we estimated too short | 208 | // we estimated too short | ||
| 216 | // add letters while text < label | 209 | // add letters while text < label | ||
| 217 | do { | 210 | do { | ||
| 218 | letters++; | 211 | letters++; | ||
| 219 | squeezedText = text.left(letters) + "..." + text.right(letters); | 212 | squeezedText = text.left(letters) + "..." + text.right(letters); | ||
| 220 | squeezedWidth = fm.width(squeezedText); | 213 | squeezedWidth = fm.width(squeezedText); | ||
| 221 | } while (squeezedWidth < labelWidth); | 214 | } while (squeezedWidth < labelWidth); | ||
| 222 | letters--; | 215 | letters--; | ||
| 223 | squeezedText = text.left(letters) + "..." + text.right(letters); | 216 | squeezedText = text.left(letters) + "..." + text.right(letters); | ||
| 224 | } else if (squeezedWidth > labelWidth) { | 217 | } else if (squeezedWidth > labelWidth) { | ||
| 225 | // we estimated too long | 218 | // we estimated too long | ||
| 226 | // remove letters while text > label | 219 | // remove letters while text > label | ||
| 227 | do { | 220 | do { | ||
| 228 | letters--; | 221 | letters--; | ||
| 229 | squeezedText = text.left(letters) + "..." + text.right(letters); | 222 | squeezedText = text.left(letters) + "..." + text.right(letters); | ||
| 230 | squeezedWidth = fm.width(squeezedText); | 223 | squeezedWidth = fm.width(squeezedText); | ||
| 231 | } while (letters && squeezedWidth > labelWidth); | 224 | } while (letters && squeezedWidth > labelWidth); | ||
| 232 | } | 225 | } | ||
| 233 | 226 | | |||
| 234 | if (index >= 0) | | |||
| 235 | setTabToolTip(index, originalText); | | |||
| 236 | | ||||
| 237 | if (letters < 5) { | | |||
| 238 | // too few letters added -> we give up squeezing | | |||
| 239 | //return text; | | |||
| 240 | return squeezedText; | | |||
| 241 | } else { | | |||
| 242 | return squeezedText; | 227 | return squeezedText; | ||
| 243 | } | 228 | } | ||
| 244 | } else { | | |||
| 245 | //if( index >= 0 ) | | |||
| 246 | // removeTabToolTip( index ); | | |||
| 247 | | ||||
| 248 | return text; | | |||
| 249 | }; | | |||
| 250 | } | | |||
| 251 | 229 | | |||
| 252 | void PanelTabBar::resizeEvent(QResizeEvent *e) | 230 | void PanelTabBar::resizeEvent(QResizeEvent *e) | ||
| 253 | { | 231 | { | ||
| 254 | QTabBar::resizeEvent(e); | 232 | QTabBar::resizeEvent(e); | ||
| 255 | 233 | | |||
| 256 | layoutTabs(); | 234 | layoutTabs(); | ||
| 257 | } | 235 | } | ||
| 258 | 236 | | |||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Line(s) | 303 | { | |||
| 332 | } else { | 310 | } else { | ||
| 333 | _dragTimer->start(); | 311 | _dragTimer->start(); | ||
| 334 | } | 312 | } | ||
| 335 | } | 313 | } | ||
| 336 | 314 | | |||
| 337 | void PanelTabBar::layoutTabs() | 315 | void PanelTabBar::layoutTabs() | ||
| 338 | { | 316 | { | ||
| 339 | for (int i = 0; i < count(); i++) { | 317 | for (int i = 0; i < count(); i++) { | ||
| 340 | setTabText(i, squeeze(DISPLAY(((ListPanel*)tabData(i).toLongLong())->virtualPath()), i)); | 318 | setTabText(i, squeeze(((ListPanel*)tabData(i).toLongLong())->virtualPath(), i)); | ||
| 341 | } | 319 | } | ||
| 342 | } | 320 | } | ||
| 343 | 321 | | |||