File indexing completed on 2024-04-28 16:26:30
0001 /**************************************************************************************** 0002 begin : Feb 24 2007 0003 copyright : 2007 by Holger Danielsson (holger.danielsson@versanet.de) 0004 2008 - 2009 by Michel Ludwig (michel.ludwig@kdemail.net) 0005 *****************************************************************************************/ 0006 0007 /*************************************************************************** 0008 * * 0009 * This program is free software; you can redistribute it and/or modify * 0010 * it under the terms of the GNU General Public License as published by * 0011 * the Free Software Foundation; either version 2 of the License, or * 0012 * (at your option) any later version. * 0013 * * 0014 ***************************************************************************/ 0015 0016 #include "widgets/abbreviationview.h" 0017 0018 #include <QFile> 0019 #include <QHeaderView> 0020 #include <QLabel> 0021 #include <QTextStream> 0022 0023 #include <KLocalizedString> 0024 #include <QMenu> 0025 #include <KMessageBox> 0026 0027 #include "abbreviationmanager.h" 0028 #include "dialogs/abbreviationinputdialog.h" 0029 #include "kiledebug.h" 0030 0031 namespace KileWidget { 0032 0033 AbbreviationView::AbbreviationView(KileAbbreviation::Manager *manager, QWidget *parent) 0034 : QTreeWidget(parent), m_abbreviationManager(manager) 0035 { 0036 setColumnCount(2); 0037 QStringList headerLabelList; 0038 headerLabelList << i18n("Short") << QString() << i18n("Expanded Text"); 0039 setHeaderLabels(headerLabelList); 0040 setAllColumnsShowFocus(true); 0041 0042 header()->setSectionsMovable(false); // default: true 0043 header()->setSectionResizeMode(QHeaderView::ResizeToContents); 0044 0045 setContextMenuPolicy(Qt::CustomContextMenu); 0046 0047 connect(this, SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(slotItemClicked(QTreeWidgetItem*,int))); 0048 connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotCustomContextMenuRequested(QPoint))); 0049 } 0050 0051 AbbreviationView::~AbbreviationView() 0052 { 0053 } 0054 //////////////////// init abbreviation view with wordlists //////////////////// 0055 0056 0057 void AbbreviationView::updateAbbreviations() 0058 { 0059 KILE_DEBUG_MAIN; 0060 setUpdatesEnabled(false); 0061 clear(); 0062 const QMap<QString, QPair<QString, bool> >& abbreviationMap = m_abbreviationManager->getAbbreviationMap(); 0063 QList<QTreeWidgetItem*> itemList; 0064 for(QMap<QString, QPair<QString, bool> >::const_iterator i = abbreviationMap.begin(); 0065 i != abbreviationMap.end(); ++i) { 0066 QPair<QString, bool> pair = i.value(); 0067 QTreeWidgetItem *item = new QTreeWidgetItem(); 0068 item->setText(ALVabbrev, i.key()); 0069 item->setText(ALVlocal, (pair.second) ? QString() : "*"); 0070 item->setText(ALVexpansion, pair.first); 0071 itemList.push_back(item); 0072 } 0073 addTopLevelItems(itemList); 0074 0075 setUpdatesEnabled(true); 0076 } 0077 0078 //////////////////// find abbreviation //////////////////// 0079 0080 bool AbbreviationView::findAbbreviation(const QString &abbrev) 0081 { 0082 QTreeWidgetItemIterator it(this); 0083 while(*it) { 0084 QTreeWidgetItem *current = *it; 0085 if(current->text(AbbreviationView::ALVabbrev) == abbrev) { 0086 return true; 0087 } 0088 0089 ++it; 0090 } 0091 return false; 0092 } 0093 0094 //////////////////// item clicked //////////////////// 0095 0096 void AbbreviationView::slotItemClicked(QTreeWidgetItem *item, int /* column */) 0097 { 0098 if(item) { 0099 QString s = item->text(AbbreviationView::ALVexpansion); 0100 s.replace("%n","\n"); 0101 emit( sendText(s) ); 0102 } 0103 } 0104 0105 0106 //////////////////// context menu //////////////////// 0107 0108 void AbbreviationView::slotCustomContextMenuRequested(const QPoint& p) 0109 { 0110 QMenu popupMenu; 0111 QAction *action = new QAction(i18n("&Add"), &popupMenu); 0112 connect(action, SIGNAL(triggered()), this, SLOT(slotAddAbbreviation())); 0113 popupMenu.addAction(action); 0114 0115 QList<QTreeWidgetItem*> selectedList = selectedItems(); 0116 if(selectedList.count() > 0) { 0117 QTreeWidgetItem *selectedItem = selectedList.first(); 0118 if(!selectedItem->text(ALVlocal).isEmpty()) { 0119 popupMenu.addSeparator(); 0120 action = new QAction(i18n("&Edit"), &popupMenu); 0121 connect(action, SIGNAL(triggered()), this, SLOT(slotChangeAbbreviation())); 0122 popupMenu.addAction(action); 0123 popupMenu.addSeparator(); 0124 action = new QAction(i18n("&Delete"), &popupMenu); 0125 connect(action, SIGNAL(triggered()), this, SLOT(slotDeleteAbbreviation())); 0126 popupMenu.addAction(action); 0127 } 0128 } 0129 0130 0131 popupMenu.exec(mapToGlobal(p)); 0132 } 0133 0134 void AbbreviationView::slotAddAbbreviation() 0135 { 0136 KileDialog::AbbreviationInputDialog dialog(this, Q_NULLPTR, ALVadd); 0137 if(dialog.exec() == QDialog::Accepted) { 0138 QString abbrev, expansion; 0139 dialog.abbreviation(abbrev, expansion); 0140 m_abbreviationManager->updateLocalAbbreviation(abbrev, expansion); 0141 } 0142 } 0143 0144 void AbbreviationView::slotChangeAbbreviation() 0145 { 0146 QList<QTreeWidgetItem*> selectedList = selectedItems(); 0147 if(selectedList.count() == 0) { 0148 return; 0149 } 0150 QTreeWidgetItem *selectedItem = selectedList.first(); 0151 QString oldAbbreviationText = selectedItem->text(ALVabbrev); 0152 QString oldAbbreviationExpansion = selectedItem->text(ALVexpansion); 0153 KileDialog::AbbreviationInputDialog dialog(this, selectedItem, ALVedit); 0154 if(dialog.exec() == QDialog::Accepted) { 0155 QString abbrev, expansion; 0156 dialog.abbreviation(abbrev, expansion); 0157 if(oldAbbreviationText != abbrev) { 0158 m_abbreviationManager->removeLocalAbbreviation(oldAbbreviationText); 0159 } 0160 m_abbreviationManager->updateLocalAbbreviation(abbrev, expansion); 0161 } 0162 } 0163 0164 void AbbreviationView::slotDeleteAbbreviation() 0165 { 0166 QList<QTreeWidgetItem*> selectedList = selectedItems(); 0167 if(selectedList.count() == 0) { 0168 return; 0169 } 0170 QTreeWidgetItem *item = selectedList.first(); 0171 QString abbreviationText = item->text(ALVabbrev); 0172 QString abbreviationExpansion = item->text(ALVexpansion); 0173 QString message = i18n("Delete the abbreviation '%1'?", abbreviationText); 0174 if(KMessageBox::questionTwoActions(this, 0175 "<center>" + message + "</center>", 0176 i18n("Delete Abbreviation"), 0177 KStandardGuiItem::del(), 0178 KStandardGuiItem::cancel()) == KMessageBox::PrimaryAction) { 0179 QString s = abbreviationText + '=' + abbreviationExpansion; 0180 } 0181 m_abbreviationManager->removeLocalAbbreviation(abbreviationText); 0182 } 0183 0184 0185 } 0186