[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [plasmate/terietor/kconfigxt] editors/kconfigxt: Make the kconfigxteditor able to modify groups and
From:       Giorgos Tsiapaliwkas <terietor () gmail ! com>
Date:       2012-07-15 18:40:28
Message-ID: 20120715184028.D9A2FA6094 () git ! kde ! org
[Download RAW message or body]

Git commit e96204a6df9f9c3c5ed49f635e6ae04e8779aaa3 by Giorgos Tsiapaliwkas.
Committed on 15/07/2012 at 20:37.
Pushed by tsiapaliwkas into branch 'terietor/kconfigxt'.

Make the kconfigxteditor able to modify groups and entries.
Note: the modification of groups works and as it regards the entries
the modification of the keys and types is correct, I don't know about
the modification of the values due to the fact that I can't test it
due to the bug which doesn't give me the entry's value.

M  +96   -52   editors/kconfigxt/kconfigxteditor.cpp
M  +7    -2    editors/kconfigxt/kconfigxteditor.h

http://commits.kde.org/plasmate/e96204a6df9f9c3c5ed49f635e6ae04e8779aaa3

diff --git a/editors/kconfigxt/kconfigxteditor.cpp \
b/editors/kconfigxt/kconfigxteditor.cpp index bbca6e4..019d7d3 100644
--- a/editors/kconfigxt/kconfigxteditor.cpp
+++ b/editors/kconfigxt/kconfigxteditor.cpp
@@ -34,6 +34,9 @@ KConfigXtEditor::KConfigXtEditor(QWidget *parent)
 {
     m_ui.setupUi(this);
 
+    //don't move the columns!!!
+    m_ui.twEntries->header()->setMovable(false);
+
     m_ui.twEntries->header()->setResizeMode(QHeaderView::ResizeToContents);
     m_ui.twGroups->header()->setResizeMode(QHeaderView::ResizeToContents);
 
@@ -45,7 +48,7 @@ KConfigXtEditor::KConfigXtEditor(QWidget *parent)
             this, SLOT(setupWidgetsForEntries(QTreeWidgetItem*)));
 
     connect(m_ui.twGroups, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, \
                SLOT(setLastGroupItem(QTreeWidgetItem*, int)));
-    connect(m_ui.twEntries, SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, \
SLOT(setLastEntryItem(QTreeWidgetItem*, int))); +    connect(m_ui.twEntries, \
SIGNAL(itemPressed(QTreeWidgetItem*, int)), this, \
SLOT(setLastEntryItem(QTreeWidgetItem*)));  
     connect(m_ui.twGroups, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, \
                SLOT(modifyGroup(QTreeWidgetItem*, int)));
     connect(m_ui.twEntries, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, \
SLOT(modifyEntry(QTreeWidgetItem*, int))); @@ -192,96 +195,137 @@ void \
KConfigXtEditor::takeDataFromParser(const QString& group)  }
 }
 
-void KConfigXtEditor::modifyEntry(QTreeWidgetItem* item, int column)
+void KConfigXtEditor::modifyGroup(QTreeWidgetItem* item, int column)
 {
     //check if ptr is evil
-    if (!item ) {
+    //and if the name of the group has been changed
+    if (!item || item->text(column) == m_lastGroupItem) {
         return;
     }
 
+    //take the groups
+    const QString oldGroupEntry = stringToGroupEntry(m_lastGroupItem);
+    const QString newGroupEntry = stringToGroupEntry(item->text(column));
+
+    //replace the groups in the xml
+    replaceItemsInXml(oldGroupEntry, newGroupEntry);
 }
 
-void KConfigXtEditor::modifyGroup(QTreeWidgetItem* item, int column)
+void KConfigXtEditor::setLastGroupItem(QTreeWidgetItem* item, int column)
 {
     //check if ptr is evil
-    //and if the name of the group has been changed
-    if (!item || item->text(column) == m_lastGroupItem) {
+    if (!item) {
         return;
     }
 
-    //take the groups
-    QString oldGroupEntry = stringToGroupEntry(m_lastGroupItem);
-    QString newGroupEntry = stringToGroupEntry(item->text(column));
+    m_lastGroupItem = item->text(column);
+}
 
-    //take the xml file
-    QFile xmlFile(m_filename.pathOrUrl());
+QString KConfigXtEditor::stringToGroupEntry(const QString& groupName) const
+{
+    return QString("<group name=\"%1\">").arg(groupName);
+}
 
-    if(!xmlFile.open(QIODevice::ReadWrite)) {
-        //the xml file has failed to open
+void KConfigXtEditor::modifyEntry(QTreeWidgetItem* item, int column)
+{
+    //check if ptr is evil
+    if (!item ) {
         return;
     }
 
-    QByteArray rawData = xmlFile.readAll();
+    //create the entry
+    const QString oldEntry = stringToEntryAndValue(m_lastEntryItem["name"],
+                                                   m_lastEntryItem["type"]);
 
-    //replace the data
-    rawData.replace(oldGroupEntry, newGroupEntry.toAscii());
+    if (column == 0 || column == 1) {
+        //the user has modified either the
+        //key or its type.
+        //TODO maybe it should check if the type is valid
 
-    //clear the xml fle
-    xmlFile.resize(0);
+        const QString newEntry = stringToEntryAndValue(item->text(0), \
item->text(1));  
-    //write the data
-    xmlFile.write(rawData);
+        //replace the entries in the xml
+        replaceItemsInXml(oldEntry, newEntry);
 
-    //close the file
-    xmlFile.close();
+    } else if (column == 2) {
+        QFile xmlFile(m_filename.pathOrUrl());
+
+        if(!xmlFile.open(QIODevice::ReadWrite)) {
+            return;
+        }
+
+       QByteArray text;
+
+       while (!xmlFile.atEnd()) {
+            QString line = xmlFile.readLine();
+            if (line.contains(oldEntry)) {
+                while (!line.contains("</entry>")) {
+                    //we took this from the xml spec
+                    QString startsWith = "<default>";
+                    QString endsWith = "</default>";
+                    if (line.startsWith(startsWith)
+                        && line.endsWith(endsWith)) {
+                            line.replace(oldEntry, startsWith
+                            + item->text(2) + endsWith);
+                    }
+               }
+            }
+            text.append(line);
+       }
 
+       //clear the xml fle
+       xmlFile.resize(0);
+
+       //write the data
+       xmlFile.write(text);
+
+       //close the file
+       xmlFile.close();
+    }
 }
 
-void KConfigXtEditor::setLastGroupItem(QTreeWidgetItem* item, int column)
+void KConfigXtEditor::setLastEntryItem(QTreeWidgetItem* item)
 {
     //check if ptr is evil
     if (!item) {
         return;
     }
 
-    m_lastGroupItem = item->text(column);
+    //our tree has 3 columns and those columns doesn't move
+    m_lastEntryItem["name"] = item->text(0);
+    m_lastEntryItem["type"] = item->text(1);
+    m_lastEntryItem["value"] = item->text(2);
 }
 
-QString KConfigXtEditor::stringToGroupEntry(const QString& groupName) const
+QString KConfigXtEditor::stringToEntryAndValue(const QString& entryName, const \
QString entryType)  {
-    QString tmpString;
-    tmpString.append("<group name=\"");
-    tmpString.append(groupName);
-    tmpString.append("\">");
-
-    return tmpString;
+    return QString("<entry name=\"%1\" type=\"%2\">").arg(entryName).arg(entryType);
 }
 
-
-void KConfigXtEditor::setLastEntryItem(QTreeWidgetItem* item, int column)
+void KConfigXtEditor::replaceItemsInXml(const QString& oldItem, const QString& \
newItem)  {
-    //check if ptr is evil
-    if (!item) {
+    //take the xml file
+    QFile xmlFile(m_filename.pathOrUrl());
+
+    if(!xmlFile.open(QIODevice::ReadWrite)) {
+        //the xml file has failed to open
         return;
     }
 
-  /* QTreeWidgetItem *i = m_ui.twEntries->headerItem();
-   QString columnName = i->text(column);
-
-   switch (columnName) {
-       case "Key":
-           m_lastEntryItem["name"] = item->text(column);
-           m_lastEntryItem["type"] = "";
-           m_lastEntryItem["value"] = "";
-        case "Type":
-           m_lastEntryItem["type"] = item->text(column);
-           m_lastEntryItem["name"] = "";
-           m_lastEntryItem["value"] = "";
-        case "Value":
-            m_lastEntryItem["value"] = item->text(column);
-            m_lastEntryItem["type"] = "";
-            m_lastEntryItem["name"] = "";
-    }*/
+    QByteArray rawData = xmlFile.readAll();
+
+    //replace the data
+    rawData.replace(oldItem, newItem.toAscii());
+
+    //clear the xml fle
+    xmlFile.resize(0);
+
+    //write the data
+    xmlFile.write(rawData);
+
+    //close the file
+    xmlFile.close();
+
 }
 
 void KConfigXtEditor::removeGroup()
diff --git a/editors/kconfigxt/kconfigxteditor.h \
b/editors/kconfigxt/kconfigxteditor.h index 2673502..046bd92 100644
--- a/editors/kconfigxt/kconfigxteditor.h
+++ b/editors/kconfigxt/kconfigxteditor.h
@@ -107,7 +107,7 @@ private slots:
     /**
      * sets the last item of the entry treewidget
      **/
-    void setLastEntryItem(QTreeWidgetItem* item, int column);
+    void setLastEntryItem(QTreeWidgetItem* item);
 
 protected:
     Ui::KConfigXtEditor m_ui;
@@ -161,7 +161,12 @@ private:
     //avoid duplication
     QString stringToGroupEntry(const QString& groupName) const;
 
-    //QHash m_lastEntryItem;
+    QHash<QString, QString> m_lastEntryItem;
+    //avoid duplication
+    QString stringToEntryAndValue(const QString& entryName, const QString \
entryType); +
+    //avoid duplication
+    void replaceItemsInXml(const QString& oldItem, const QString& newItem);
 };
 
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic