File indexing completed on 2024-04-28 16:26:30

0001 /********************************************************************************
0002 *   Copyright (C) 2018 by Michel Ludwig (michel.ludwig@kdemail.net)             *
0003 *                 2009 by Holger Danielsson (holger.danielsson@versanet.de)     *
0004 *********************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 
0016 #include "commandview.h"
0017 
0018 #include <KLocalizedString>
0019 #include <KTextEditor/View>
0020 
0021 #include <QComboBox>
0022 #include <QVBoxLayout>
0023 
0024 #include <algorithm>
0025 
0026 #include "kileconfig.h"
0027 #include "kileviewmanager.h"
0028 #include "kiledebug.h"
0029 
0030 
0031 namespace KileWidget {
0032 
0033 //-------------------- CommandView --------------------
0034 
0035 CommandView::CommandView(QWidget *parent)
0036     : QListWidget(parent)
0037 {
0038     setViewMode(ListMode);
0039     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0040     setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
0041     setSortingEnabled(true);
0042     setDragDropMode(NoDragDrop);
0043 
0044     connect(this, SIGNAL(itemActivated(QListWidgetItem*)), parent, SLOT(slotItemActivated(QListWidgetItem*)));
0045 }
0046 
0047 CommandView::~CommandView()
0048 {
0049 }
0050 
0051 //-------------------- CommandViewToolBox --------------------
0052 
0053 CommandViewToolBox::CommandViewToolBox(KileInfo *ki, QWidget *parent)
0054     : QWidget(parent), m_ki(ki)
0055 {
0056     // we need a completion model for some auxiliary functions
0057     m_latexCompletionModel = new KileCodeCompletion::LaTeXCompletionModel(this,
0058             m_ki->codeCompletionManager(),
0059             m_ki->editorExtension());
0060     m_cwlFilesComboBox = new QComboBox(this);
0061     connect(m_cwlFilesComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
0062     [=](int index) {
0063         populateCommands(m_cwlFilesComboBox->itemData(index).toString());
0064     });
0065 
0066     m_commandView = new CommandView(this);
0067 
0068     QVBoxLayout *layout = new QVBoxLayout();
0069     layout->addWidget(m_cwlFilesComboBox);
0070     layout->addWidget(m_commandView);
0071 
0072     setLayout(layout);
0073 
0074     clearItems();
0075 }
0076 
0077 CommandViewToolBox::~CommandViewToolBox()
0078 {
0079 }
0080 
0081 void CommandViewToolBox::readCommandViewFiles()
0082 {
0083     clearItems();
0084 
0085     KileCodeCompletion::Manager *manager = m_ki->codeCompletionManager();
0086 
0087     QStringList validCwlFiles;
0088 
0089     for (const QString& file : KileConfig::completeTex()) {
0090         // check, if the wordlist has to be read
0091         const QString validCwlFile = manager->validCwlFile(file);
0092 
0093         if(!validCwlFile.isEmpty()) {
0094             validCwlFiles << validCwlFile;
0095         }
0096     }
0097 
0098     std::sort(validCwlFiles.begin(), validCwlFiles.end());
0099 
0100     for (const QString& cwlFile : qAsConst(validCwlFiles)) {
0101         m_cwlFilesComboBox->addItem(cwlFile, cwlFile);
0102     }
0103 
0104     if(m_cwlFilesComboBox->count() > 0) {
0105         m_commandView->setEnabled(true);
0106         m_cwlFilesComboBox->setEnabled(true);
0107         m_cwlFilesComboBox->setCurrentIndex(0);
0108     }
0109 }
0110 
0111 void CommandViewToolBox::populateCommands(const QString& cwlFile)
0112 {
0113     KileCodeCompletion::Manager *manager = m_ki->codeCompletionManager();
0114 
0115     m_commandView->clear();
0116 
0117     const QStringList wordlist = manager->readCWLFile("tex/" + cwlFile + ".cwl");
0118 
0119     for(QString string : wordlist) {
0120         m_commandView->addItem(string);
0121     }
0122 }
0123 
0124 void CommandViewToolBox::clearItems()
0125 {
0126     m_commandView->clear();
0127     m_cwlFilesComboBox->clear();
0128 
0129     m_commandView->setEnabled(false);
0130     m_cwlFilesComboBox->setEnabled(false);
0131 }
0132 
0133 void CommandViewToolBox::slotItemActivated(QListWidgetItem *item)
0134 {
0135     KTextEditor::View *view = m_ki->viewManager()->currentTextView();
0136     if(view) {
0137         KTextEditor::Cursor cursor = view->cursorPosition();
0138 
0139         //insert text
0140         int xpos,ypos;
0141         QString text = m_latexCompletionModel->filterLatexCommand(item->text(),ypos,xpos);
0142         if(!text.isEmpty()) {
0143             emit(sendText(text));
0144 
0145             // place cursor
0146             if(KileConfig::completeCursor() && (xpos > 0 || ypos > 0) ) {
0147                 view->setCursorPosition(KTextEditor::Cursor(cursor.line() + (ypos >= 0 ? ypos : 0),
0148                                         cursor.column() + (xpos >= 0 ? xpos : 0)));
0149             }
0150         }
0151     }
0152 }
0153 
0154 }