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

List:       kde-commits
Subject:    [plasmate/terietor/kconfigxt] editors/kconfigxt: Implement the remove functionality and remove the d
From:       Giorgos Tsiapaliwkas <terietor () gmail ! com>
Date:       2012-07-11 18:32:43
Message-ID: 20120711183243.ECDF7A60C6 () git ! kde ! org
[Download RAW message or body]

Git commit 5124c6426a364fe056e531a069e771f1a51a6763 by Giorgos Tsiapaliwkas.
Committed on 01/07/2012 at 17:46.
Pushed by tsiapaliwkas into branch 'terietor/kconfigxt'.

Implement the remove functionality and remove the debug code

M  +81   -13   editors/kconfigxt/kconfigxteditor.cpp
M  +18   -0    editors/kconfigxt/kconfigxteditor.h
M  +1    -1    editors/kconfigxt/kconfigxteditor.ui
M  +1    -10   editors/kconfigxt/kconfigxtparser.cpp

http://commits.kde.org/plasmate/5124c6426a364fe056e531a069e771f1a51a6763

diff --git a/editors/kconfigxt/kconfigxteditor.cpp \
b/editors/kconfigxt/kconfigxteditor.cpp index 8f48282..f725b88 100644
--- a/editors/kconfigxt/kconfigxteditor.cpp
+++ b/editors/kconfigxt/kconfigxteditor.cpp
@@ -22,6 +22,9 @@
 
 #include <KDebug>
 #include <KIcon>
+#include <KMessageBox>
+#include <KLocalizedString>
+
 #include <QFile>
 #include <QHeaderView>
 #include <QTreeWidgetItem>
@@ -40,6 +43,7 @@ KConfigXtEditor::KConfigXtEditor(QWidget *parent)
     connect(m_ui.twGroups, SIGNAL(currentItemChanged(QTreeWidgetItem*, \
QTreeWidgetItem*)),  this, SLOT(setupWidgetsForEntries(QTreeWidgetItem*)));
     connect(m_ui.pbDeleteGroup, SIGNAL(clicked()), this, SLOT(removeGroup()));
+    connect(m_ui.pbDeleteEntry, SIGNAL(clicked()), this, SLOT(removeEntry()));
 
     //hide the source related ui stuff
     m_ui.srcLabel1->setVisible(false);
@@ -176,9 +180,6 @@ void KConfigXtEditor::addGroupToUi(const QString& group)
 
 void KConfigXtEditor::addEntryToUi(const QString& key, const QString& type, const \
QString& value)  {
-    qDebug() << "key:" + key;
-    qDebug() << "type:" + type;
-    qDebug() << "value:" + value;
     QTreeWidgetItem *item = new QTreeWidgetItem();
     item->setText(0, key);
     item->setText(1, type);
@@ -190,25 +191,92 @@ void KConfigXtEditor::addEntryToUi(const QString& key, const \
QString& type, cons  
 void KConfigXtEditor::removeGroup()
 {
-    QByteArray array;
-    
+    //take the current item of the tree
     QTreeWidgetItem *item = m_ui.twGroups->currentItem();
+    if (removeElement(item->text(0), KConfigXtEditor::Group)) {
+        m_ui.twGroups->removeItemWidget(item, 0);
+    } else {
+        removeError();
+    }
+}
+
+void KConfigXtEditor::removeEntry()
+{
+    //take the current item of the tree
+    QTreeWidgetItem *item = m_ui.twEntries->currentItem();
+    if (removeElement(item->text(0), KConfigXtEditor::Entry)) {
+        m_ui.twEntries->removeItemWidget(item, 0);
+    } else {
+        removeError();
+    }
+}
+
+void KConfigXtEditor::removeError()
+{
+    QString error = i18n("An error has occured during the removal of the item");
+    KMessageBox::error(this, error);
+}
 
-    QString group = "<group name=\"" + item->text(0) + "\">";
+
+bool KConfigXtEditor::removeElement(const QString& elementName, ElementType \
elementType) +{
+    if (elementName.isEmpty()) {
+        return false;
+    }
+
+    //take the element that we want
+    QString startElement;
+    if (elementType == KConfigXtEditor::Group) {
+        startElement  = "<group name=\"" + elementName + "\">";
+    } else if (elementType == KConfigXtEditor::Entry) {
+        startElement  = "<entry name=\"" + elementName + "\">";
+    }
 
     QFile xmlFile(m_filename.pathOrUrl());
     if(!xmlFile.open(QIODevice::ReadWrite)) {
-        return;
+        return false;
     }
 
-    QTextStream text(&xmlFile);
+    //Let me exaplain what is going on in this method,
+    //there is no way in Qt to remove the contents of
+    //a file if we don't use QTextEdit. So we can only keep
+    //the content which we don't want to delete then to empty
+    //our file and finally to add the content which we don't
+    //want to delete. So, its like we delete something!
+    QByteArray text;
+
+    while (!xmlFile.atEnd()) {
+        QString line = xmlFile.readLine();
+        //while the element has started and the element
+        //hasn't ended skip those lines.
+        if (line.contains(startElement)) {
+            //choose our flag according to the give type;
+            QString endElement;
+            if (elementType == KConfigXtEditor::Group) {
+                endElement = "</group>";
+            } else if (elementType == KConfigXtEditor::Entry) {
+                endElement = "</entry>";
+            }
+
+            while (!line.contains(endElement)) {
+                line = xmlFile.readLine();
+            }
 
-    while (!text.atEnd()) {
-        QString line = text.readLine();
-        if (line == group) {
-            while (line != "</group>") {
-                array.replace(line,"");
+            //if you are the last element which
+            //you have escaped from the while, skip!
+            if (line.contains(endElement)) {
+                line.clear();
             }
         }
+        text.append(line);
     }
+
+    //empty the xml file
+    xmlFile.resize(0);
+
+    //write our text and close the file
+    xmlFile.write(text);
+    xmlFile.close();
+
+    return true;
 }
diff --git a/editors/kconfigxt/kconfigxteditor.h \
b/editors/kconfigxt/kconfigxteditor.h index d137e9a..0983481 100644
--- a/editors/kconfigxt/kconfigxteditor.h
+++ b/editors/kconfigxt/kconfigxteditor.h
@@ -36,6 +36,11 @@ class KConfigXtEditor : public QWidget
 public:
     KConfigXtEditor(QWidget *parent = 0);
 
+    enum ElementType {
+        Group = 0,
+        Entry
+    };
+
     /**
      * Sets filename to edit
      */
@@ -79,6 +84,11 @@ private slots:
      **/
     void removeGroup();
 
+    /**
+     * Removes an entry from the xml file
+     **/
+    void removeEntry();
+
 protected:
     Ui::KConfigXtEditor m_ui;
 
@@ -109,6 +119,14 @@ private:
     void addEntryToUi(const QString& entryName,
                                 const QString& entryType, const QString& \
entryValue);  
+    //with this method we can avoid duplication,
+    //this method will delete everything that is between the startsWith
+    //and endsWith
+    bool removeElement(const QString& elementName, ElementType elementType);
+
+    //reduces duplication
+    void removeError();
+
     KUrl m_filename;
     QStringList m_groups;
     KConfigXtParserItem m_keysValuesTypes;
diff --git a/editors/kconfigxt/kconfigxteditor.ui \
b/editors/kconfigxt/kconfigxteditor.ui index 370d4b2..f1ec3a0 100644
--- a/editors/kconfigxt/kconfigxteditor.ui
+++ b/editors/kconfigxt/kconfigxteditor.ui
@@ -114,7 +114,7 @@
      <item row="1" column="2">
       <widget class="KPushButton" name="pbDeleteGroup">
        <property name="text">
-        <string>Delete Grop</string>
+        <string>Delete Group</string>
        </property>
       </widget>
      </item>
diff --git a/editors/kconfigxt/kconfigxtparser.cpp \
b/editors/kconfigxt/kconfigxtparser.cpp index 990eed8..52b8d0a 100644
--- a/editors/kconfigxt/kconfigxtparser.cpp
+++ b/editors/kconfigxt/kconfigxtparser.cpp
@@ -26,9 +26,9 @@
 #include <QFile>
 #include <QXmlStreamReader>
 
-#include <QDebug>
 KConfigXtParserItem::KConfigXtParserItem(QObject* parent)
 {
+    Q_UNUSED(parent)
 }
 
 QString KConfigXtParserItem::groupName() const
@@ -54,13 +54,11 @@ QString KConfigXtParserItem::entryValue() const
 void KConfigXtParserItem::setGroupName(const QString& groupName)
 {
     m_groupName = groupName;
-    qDebug() << "groupName:" + groupName;
 }
 
 void KConfigXtParserItem::setEntryName(const QString& entryName)
 {
     m_entryName = entryName;
-    qDebug() << "entryName:" + entryName;
 }
 
 void KConfigXtParserItem::setEntryType(const QString& entryType)
@@ -97,13 +95,11 @@ void KConfigXtParserItem::setEntryType(const QString& entryType)
             m_entryType = type;
         }
     }
-    qDebug() << "entryType:" + entryType;
 }
 
 void KConfigXtParserItem::setEntryValue(const QString& entryValue)
 {
     m_entryValue = entryValue;
-    qDebug() << "m_entryValue:" + m_entryValue;
 }
 
 KConfigXtParser::KConfigXtParser(QObject *parent)
@@ -175,7 +171,6 @@ void KConfigXtParser::parseGroup(QXmlStreamReader& reader)
     if(reader.attributes().hasAttribute("name")) {
     // We'll add it to the hash
     m_data.setGroupName(reader.attributes().value("name").toString());
-    qDebug() << "auto einai to name tou group:" + \
reader.attributes().value("name").toString();  }
 
 
@@ -204,15 +199,12 @@ void KConfigXtParser::parseEntry(QXmlStreamReader& reader)
     //check if there is a type attribute
     //if there isn't fail!
     if (reader.attributes().hasAttribute("type")) {
-        qDebug() << "auto einai to attribute  type tou entry:" + \
reader.attributes().value("type").toString();  //now we can take the entry's name and \
                type
         m_data.setEntryName(reader.attributes().value("name").toString());
         m_data.setEntryType(reader.attributes().value("type").toString());
 
         m_parseResult = true;
 
-        qDebug() << "entry name:" + reader.name().toString();
-        qDebug() << "entry type:" + reader.attributes().value("type").toString();
     } else {
         //there is no type, fail
         m_parseResult = false;
@@ -230,7 +222,6 @@ void KConfigXtParser::parseEntry(QXmlStreamReader& reader)
 
         //we have a default element
         if (reader.name().toString() == "default") {
-            qDebug() << "mpike!!!";
 
             //go ahead one more!
             reader.readNext();


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

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