Changeset View
Changeset View
Standalone View
Standalone View
src/mode/katemodemenulist.cpp
Show First 20 Lines • Show All 133 Lines • ▼ Show 20 Line(s) | 132 | } else { | |||
---|---|---|---|---|---|
134 | m_list->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); | 134 | m_list->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); | ||
135 | } | 135 | } | ||
136 | m_list->setIconSize(QSize(m_iconSize, m_iconSize)); | 136 | m_list->setIconSize(QSize(m_iconSize, m_iconSize)); | ||
137 | m_list->setResizeMode(QListView::Adjust); | 137 | m_list->setResizeMode(QListView::Adjust); | ||
138 | // Size of the list widget and search bar. | 138 | // Size of the list widget and search bar. | ||
139 | setSizeList(menuHeight, menuWidth); | 139 | setSizeList(menuHeight, menuWidth); | ||
140 | 140 | | |||
141 | // Data model (items). | 141 | // Data model (items). | ||
142 | // couple model to view to let it be deleted with the view | ||||
143 | m_model = new QStandardItemModel(0, 0, m_list); | ||||
142 | loadHighlightingModel(); | 144 | loadHighlightingModel(); | ||
143 | 145 | | |||
144 | /* | 146 | /* | ||
145 | * Search bar widget. | 147 | * Search bar widget. | ||
146 | */ | 148 | */ | ||
147 | m_searchBar->setPlaceholderText(i18nc("Placeholder in search bar", "Search...")); | 149 | m_searchBar->setPlaceholderText(i18nc("Placeholder in search bar", "Search...")); | ||
148 | m_searchBar->setToolTip(i18nc("ToolTip of the search bar of modes of syntax highlighting", "Search for syntax highlighting modes by language name or file extension (for example, C++ or .cpp)")); | 150 | m_searchBar->setToolTip(i18nc("ToolTip of the search bar of modes of syntax highlighting", "Search for syntax highlighting modes by language name or file extension (for example, C++ or .cpp)")); | ||
149 | m_searchBar->setMaxLength(200); | 151 | m_searchBar->setMaxLength(200); | ||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Line(s) | 202 | #endif | |||
207 | 209 | | |||
208 | /* | 210 | /* | ||
209 | * Detect selected item with one click. | 211 | * Detect selected item with one click. | ||
210 | * This also applies to double-clicks. | 212 | * This also applies to double-clicks. | ||
211 | */ | 213 | */ | ||
212 | connect(m_list, &KateModeMenuListData::ListView::clicked, this, &KateModeMenuList::selectHighlighting); | 214 | connect(m_list, &KateModeMenuListData::ListView::clicked, this, &KateModeMenuList::selectHighlighting); | ||
213 | } | 215 | } | ||
214 | 216 | | |||
217 | void KateModeMenuList::reloadItems() | ||||
218 | { | ||||
219 | const QString searchText = m_searchBar->text().trimmed(); | ||||
220 | m_searchBar->m_bestResults.clear(); | ||||
221 | if (!isHidden()) { | ||||
222 | hide(); | ||||
223 | } | ||||
224 | /* | ||||
225 | * Clear model. | ||||
226 | * NOTE: This deletes the item objects and widgets indexed to items. | ||||
227 | * That is, the QLabel & QFrame objects of the section titles are also deleted. | ||||
228 | * See: QAbstractItemView::setIndexWidget(), QObject::deleteLater() | ||||
229 | */ | ||||
230 | m_model->clear(); | ||||
231 | m_list->selectionModel()->clear(); | ||||
232 | m_selectedItem = nullptr; | ||||
233 | | ||||
234 | loadHighlightingModel(); | ||||
235 | | ||||
236 | // Restore search text, if there is. | ||||
237 | m_searchBar->m_bSearchStateAutoScroll = false; | ||||
238 | if (!searchText.isEmpty()) { | ||||
239 | selectHighlightingFromExternal(); | ||||
240 | m_searchBar->updateSearch(searchText); | ||||
241 | m_searchBar->setText(searchText); | ||||
242 | } | ||||
243 | } | ||||
244 | | ||||
215 | void KateModeMenuList::loadHighlightingModel() | 245 | void KateModeMenuList::loadHighlightingModel() | ||
216 | { | 246 | { | ||
217 | // couple model to view to let it be deleted with the view | | |||
218 | m_model = new QStandardItemModel(0, 0, m_list); | | |||
219 | m_list->setModel(m_model); | 247 | m_list->setModel(m_model); | ||
220 | 248 | | |||
221 | QString *prevHlSection = nullptr; | 249 | QString *prevHlSection = nullptr; | ||
222 | /* | 250 | /* | ||
223 | * The width of the text container in the item, in pixels. This is used to make | 251 | * The width of the text container in the item, in pixels. This is used to make | ||
224 | * a custom word wrap and prevent the item's text from passing under the scroll bar. | 252 | * a custom word wrap and prevent the item's text from passing under the scroll bar. | ||
225 | * NOTE: 8 = Icon margin | 253 | * NOTE: 8 = Icon margin | ||
226 | */ | 254 | */ | ||
Show All 13 Lines | |||||
240 | m_list->setRowHidden(0, true); | 268 | m_list->setRowHidden(0, true); | ||
241 | 269 | | |||
242 | /* | 270 | /* | ||
243 | * Get list of modes from KateModeManager::list(). | 271 | * Get list of modes from KateModeManager::list(). | ||
244 | * We assume that the modes are arranged according to sections, alphabetically; | 272 | * We assume that the modes are arranged according to sections, alphabetically; | ||
245 | * and the attribute "translatedSection" isn't empty if "section" has a value. | 273 | * and the attribute "translatedSection" isn't empty if "section" has a value. | ||
246 | */ | 274 | */ | ||
247 | for (auto *hl : KTextEditor::EditorPrivate::self()->modeManager()->list()) { | 275 | for (auto *hl : KTextEditor::EditorPrivate::self()->modeManager()->list()) { | ||
276 | if (hl->name.isEmpty()) { | ||||
277 | continue; | ||||
278 | } | ||||
279 | | ||||
248 | // Detects a new section. | 280 | // Detects a new section. | ||
249 | if (!hl->translatedSection.isEmpty() && (prevHlSection == nullptr || hl->translatedSection != *prevHlSection)) { | 281 | if (!hl->translatedSection.isEmpty() && (prevHlSection == nullptr || hl->translatedSection != *prevHlSection)) { | ||
250 | createSectionList(hl->sectionTranslated(), transparentBrush); | 282 | createSectionList(hl->sectionTranslated(), transparentBrush); | ||
251 | } | 283 | } | ||
252 | prevHlSection = hl->translatedSection.isNull() ? nullptr : &hl->translatedSection; | 284 | prevHlSection = hl->translatedSection.isNull() ? nullptr : &hl->translatedSection; | ||
253 | 285 | | |||
254 | // Create item in the list with the language name. | 286 | // Create item in the list with the language name. | ||
255 | KateModeMenuListData::ListItem *item = KateModeMenuListData::Factory::createListItem(); | 287 | KateModeMenuListData::ListItem *item = KateModeMenuListData::Factory::createListItem(); | ||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Line(s) | |||||
369 | void KateModeMenuList::setSizeList(const int height, const int width) | 401 | void KateModeMenuList::setSizeList(const int height, const int width) | ||
370 | { | 402 | { | ||
371 | m_list->setSizeList(height, width); | 403 | m_list->setSizeList(height, width); | ||
372 | m_searchBar->setWidth(width); | 404 | m_searchBar->setWidth(width); | ||
373 | } | 405 | } | ||
374 | 406 | | |||
375 | void KateModeMenuList::autoScroll() | 407 | void KateModeMenuList::autoScroll() | ||
376 | { | 408 | { | ||
377 | if (m_autoScroll == ScrollToSelectedItem) { | 409 | if (m_selectedItem && m_autoScroll == ScrollToSelectedItem) { | ||
378 | m_list->setCurrentItem(m_selectedItem->row()); | 410 | m_list->setCurrentItem(m_selectedItem->row()); | ||
379 | m_list->scrollToItem(m_selectedItem->row(), QAbstractItemView::PositionAtCenter); | 411 | m_list->scrollToItem(m_selectedItem->row(), QAbstractItemView::PositionAtCenter); | ||
380 | } else { | 412 | } else { | ||
381 | m_list->setCurrentItem(0); | 413 | m_list->scrollToFirstItem(); | ||
382 | m_list->scrollToTop(); | | |||
383 | } | 414 | } | ||
384 | } | 415 | } | ||
385 | 416 | | |||
386 | void KateModeMenuList::showEvent(QShowEvent *event) | 417 | void KateModeMenuList::showEvent(QShowEvent *event) | ||
387 | { | 418 | { | ||
388 | Q_UNUSED(event); | 419 | Q_UNUSED(event); | ||
389 | /* | 420 | /* | ||
390 | * TODO: Put the menu on the bottom-edge of the window if the status bar is hidden, | 421 | * TODO: Put the menu on the bottom-edge of the window if the status bar is hidden, | ||
Show All 37 Lines | 428 | if (m_pushButton && m_pushButton->isVisible()) { | |||
428 | } else if (m_positionY == AlignTop) { | 459 | } else if (m_positionY == AlignTop) { | ||
429 | // Set vertical position, use the default horizontal position | 460 | // Set vertical position, use the default horizontal position | ||
430 | move(pos().x(), newMenu_y); | 461 | move(pos().x(), newMenu_y); | ||
431 | } | 462 | } | ||
432 | } | 463 | } | ||
433 | 464 | | |||
434 | // Select text from the search bar | 465 | // Select text from the search bar | ||
435 | if (!m_searchBar->text().isEmpty()) { | 466 | if (!m_searchBar->text().isEmpty()) { | ||
436 | if (m_searchBar->text().simplified().isEmpty()) { | 467 | if (m_searchBar->text().trimmed().isEmpty()) { | ||
437 | m_searchBar->clear(); | 468 | m_searchBar->clear(); | ||
438 | } else { | 469 | } else { | ||
439 | m_searchBar->selectAll(); | 470 | m_searchBar->selectAll(); | ||
440 | } | 471 | } | ||
441 | } | 472 | } | ||
442 | 473 | | |||
443 | // Set focus on the list. The list widget uses focus proxy to the search bar. | 474 | // Set focus on the list. The list widget uses focus proxy to the search bar. | ||
444 | m_list->setFocus(Qt::ActiveWindowFocusReason); | 475 | m_list->setFocus(Qt::ActiveWindowFocusReason); | ||
445 | 476 | | |||
446 | KTextEditor::DocumentPrivate *doc = m_doc; | 477 | KTextEditor::DocumentPrivate *doc = m_doc; | ||
447 | if (!doc) { | 478 | if (!doc) { | ||
448 | return; | 479 | return; | ||
449 | } | 480 | } | ||
450 | 481 | | |||
451 | // First show or if an external changed the current syntax highlighting. | 482 | // First show or if an external changed the current syntax highlighting. | ||
452 | if (!m_selectedItem || (m_selectedItem->hasMode() && m_selectedItem->getMode()->name != doc->fileType())) { | 483 | if (!m_selectedItem || (m_selectedItem->hasMode() && m_selectedItem->getMode()->name != doc->fileType())) { | ||
453 | selectHighlightingFromExternal(doc->fileType()); | 484 | if (!selectHighlightingFromExternal(doc->fileType())) { | ||
485 | // Strange case: if the current syntax highlighting does not exist in the list. | ||||
486 | if (m_selectedItem) { | ||||
487 | m_selectedItem->setIcon(m_emptyIcon); | ||||
488 | } | ||||
489 | if ((m_selectedItem || !m_list->currentItem()) && m_searchBar->text().isEmpty()) { | ||||
490 | m_list->scrollToFirstItem(); | ||||
491 | } | ||||
492 | m_selectedItem = nullptr; | ||||
493 | } | ||||
454 | } | 494 | } | ||
455 | } | 495 | } | ||
456 | 496 | | |||
457 | void KateModeMenuList::updateSelectedItem(KateModeMenuListData::ListItem *item) | 497 | void KateModeMenuList::updateSelectedItem(KateModeMenuListData::ListItem *item) | ||
458 | { | 498 | { | ||
459 | // Change the previously selected item to empty icon | 499 | // Change the previously selected item to empty icon | ||
460 | if (m_selectedItem) { | 500 | if (m_selectedItem) { | ||
461 | m_selectedItem->setIcon(m_emptyIcon); | 501 | m_selectedItem->setIcon(m_emptyIcon); | ||
462 | } | 502 | } | ||
463 | 503 | | |||
464 | // Update the selected item | 504 | // Update the selected item | ||
465 | item->setIcon(m_checkIcon); | 505 | item->setIcon(m_checkIcon); | ||
466 | m_selectedItem = item; | 506 | m_selectedItem = item; | ||
467 | m_list->setCurrentItem(item->row()); | 507 | m_list->setCurrentItem(item->row()); | ||
468 | 508 | | |||
469 | // Change text of the trigger button | 509 | // Change text of the trigger button | ||
470 | if (bool(m_autoUpdateTextButton) && m_pushButton && item->hasMode()) { | 510 | if (bool(m_autoUpdateTextButton) && m_pushButton && item->hasMode()) { | ||
471 | m_pushButton->setText(item->getMode()->nameTranslated()); | 511 | m_pushButton->setText(item->getMode()->nameTranslated()); | ||
472 | } | 512 | } | ||
473 | } | 513 | } | ||
474 | 514 | | |||
475 | void KateModeMenuList::selectHighlightingSetVisibility(QStandardItem *pItem, const bool bHideMenu) | 515 | void KateModeMenuList::selectHighlightingSetVisibility(QStandardItem *pItem, const bool bHideMenu) | ||
476 | { | 516 | { | ||
477 | if (!pItem->isSelectable() || !pItem->isEnabled()) { | 517 | if (!pItem || !pItem->isSelectable() || !pItem->isEnabled()) { | ||
478 | return; | 518 | return; | ||
479 | } | 519 | } | ||
480 | 520 | | |||
481 | KateModeMenuListData::ListItem *item = static_cast<KateModeMenuListData::ListItem *>(pItem); | 521 | KateModeMenuListData::ListItem *item = static_cast<KateModeMenuListData::ListItem *>(pItem); | ||
482 | 522 | | |||
483 | if (!item->text().isEmpty()) { | 523 | if (!item->text().isEmpty()) { | ||
484 | updateSelectedItem(item); | 524 | updateSelectedItem(item); | ||
485 | } | 525 | } | ||
486 | if (bHideMenu) { | 526 | if (bHideMenu) { | ||
487 | hide(); | 527 | hide(); | ||
488 | } | 528 | } | ||
489 | 529 | | |||
490 | // Apply syntax highlighting | 530 | // Apply syntax highlighting | ||
491 | KTextEditor::DocumentPrivate *doc = m_doc; | 531 | KTextEditor::DocumentPrivate *doc = m_doc; | ||
492 | if (doc && item->hasMode()) { | 532 | if (doc && item->hasMode()) { | ||
493 | doc->updateFileType(item->getMode()->name, true); | 533 | doc->updateFileType(item->getMode()->name, true); | ||
494 | } | 534 | } | ||
495 | } | 535 | } | ||
496 | 536 | | |||
497 | void KateModeMenuList::selectHighlighting(const QModelIndex &index) | 537 | void KateModeMenuList::selectHighlighting(const QModelIndex &index) | ||
498 | { | 538 | { | ||
499 | selectHighlightingSetVisibility(m_model->item(index.row(), 0), true); | 539 | selectHighlightingSetVisibility(m_model->item(index.row(), 0), true); | ||
500 | } | 540 | } | ||
501 | 541 | | |||
502 | void KateModeMenuList::selectHighlightingFromExternal(const QString &nameMode) | 542 | bool KateModeMenuList::selectHighlightingFromExternal(const QString &nameMode) | ||
503 | { | 543 | { | ||
504 | for (int i = 0; i < m_model->rowCount(); ++i) { | 544 | for (int i = 0; i < m_model->rowCount(); ++i) { | ||
505 | KateModeMenuListData::ListItem *item = static_cast<KateModeMenuListData::ListItem *>(m_model->item(i, 0)); | 545 | KateModeMenuListData::ListItem *item = static_cast<KateModeMenuListData::ListItem *>(m_model->item(i, 0)); | ||
506 | 546 | | |||
507 | if (!item->hasMode() || m_model->item(i, 0)->text().isEmpty()) { | 547 | if (!item->hasMode() || m_model->item(i, 0)->text().isEmpty()) { | ||
508 | continue; | 548 | continue; | ||
509 | } | 549 | } | ||
510 | if (item->getMode()->name == nameMode || (nameMode.isEmpty() && item->getMode()->name == QLatin1String("Normal"))) { | 550 | if (item->getMode()->name == nameMode || (nameMode.isEmpty() && item->getMode()->name == QLatin1String("Normal"))) { | ||
511 | updateSelectedItem(item); | 551 | updateSelectedItem(item); | ||
512 | 552 | | |||
513 | // Clear search | 553 | // Clear search | ||
514 | if (!m_searchBar->text().isEmpty()) { | 554 | if (!m_searchBar->text().isEmpty()) { | ||
515 | // Prevent the empty list message from being seen over the items for a short time | 555 | // Prevent the empty list message from being seen over the items for a short time | ||
516 | if (m_emptyListMsg) { | 556 | if (m_emptyListMsg) { | ||
517 | m_emptyListMsg->hide(); | 557 | m_emptyListMsg->hide(); | ||
518 | } | 558 | } | ||
519 | 559 | | |||
520 | // NOTE: This calls updateSearch(), it's scrolled to the selected item or the first item. | 560 | // NOTE: This calls updateSearch(), it's scrolled to the selected item or the first item. | ||
521 | m_searchBar->clear(); | 561 | m_searchBar->clear(); | ||
522 | } else if (m_autoScroll == ScrollToSelectedItem) { | 562 | } else if (m_autoScroll == ScrollToSelectedItem) { | ||
523 | m_list->scrollToItem(i); | 563 | m_list->scrollToItem(i); | ||
524 | } else { | 564 | } else { | ||
525 | // autoScroll() | 565 | // autoScroll() | ||
526 | m_list->setCurrentItem(0); | 566 | m_list->scrollToFirstItem(); | ||
527 | m_list->scrollToTop(); | | |||
528 | } | 567 | } | ||
529 | return; | 568 | return true; | ||
530 | } | 569 | } | ||
531 | } | 570 | } | ||
571 | return false; | ||||
532 | } | 572 | } | ||
533 | 573 | | |||
534 | void KateModeMenuList::selectHighlightingFromExternal() | 574 | bool KateModeMenuList::selectHighlightingFromExternal() | ||
535 | { | 575 | { | ||
536 | KTextEditor::DocumentPrivate *doc = m_doc; | 576 | KTextEditor::DocumentPrivate *doc = m_doc; | ||
537 | if (doc) { | 577 | if (doc) { | ||
538 | selectHighlightingFromExternal(doc->fileType()); | 578 | return selectHighlightingFromExternal(doc->fileType()); | ||
539 | } | 579 | } | ||
580 | return false; | ||||
540 | } | 581 | } | ||
541 | 582 | | |||
542 | void KateModeMenuList::loadEmptyMsg() | 583 | void KateModeMenuList::loadEmptyMsg() | ||
543 | { | 584 | { | ||
544 | m_emptyListMsg = new QLabel(i18nc("A search yielded no results", "No items matching your search"), this); | 585 | m_emptyListMsg = new QLabel(i18nc("A search yielded no results", "No items matching your search"), this); | ||
545 | m_emptyListMsg->setMargin(15); | 586 | m_emptyListMsg->setMargin(15); | ||
546 | m_emptyListMsg->setWordWrap(true); | 587 | m_emptyListMsg->setWordWrap(true); | ||
547 | 588 | | |||
▲ Show 20 Lines • Show All 618 Lines • Show Last 20 Lines |