Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include "newkeycommand.hpp"
10 : #include <kdb.hpp>
11 :
12 0 : NewKeyCommand::NewKeyCommand (TreeViewModel * model, int index, DataContainer * data, bool isBelow, QUndoCommand * parent)
13 0 : : QUndoCommand (parent), m_parentNode (model->model ().at (index)), m_newNode (nullptr), m_value (data->newValue ()),
14 0 : m_metaData (data->newMetadata ())
15 : {
16 0 : TreeViewModel * parentModel = m_parentNode->getChildren ();
17 0 : kdb::Key newKey = parentModel->createNewKey (m_parentNode->getPath () + "/" + data->newName (), m_value, m_metaData);
18 :
19 0 : QStringList newNameSplit = parentModel->getSplittedKeyname (newKey);
20 0 : kdb::Key parentKey = m_parentNode->getKey ();
21 :
22 0 : if (!parentKey) parentKey = kdb::Key (m_parentNode->getPath ().toStdString (), KEY_END);
23 :
24 0 : QStringList parentNameSplit = parentModel->getSplittedKeyname (parentKey);
25 :
26 : // check if the new key is directly below the parent
27 0 : QSet<QString> diff = newNameSplit.toSet ().subtract (parentNameSplit.toSet ());
28 :
29 0 : if (diff.count () > 1 || isBelow)
30 0 : setText ("newBranch");
31 : else
32 0 : setText ("newKey");
33 :
34 0 : m_name = cutListAtIndex (newNameSplit, parentNameSplit.count ()).first ();
35 :
36 0 : parentModel->sink (m_parentNode, newNameSplit, newKey.dup ());
37 :
38 0 : m_newNode = m_parentNode->getChildByName (m_name);
39 0 : parentModel->removeRow (m_parentNode->getChildIndexByName (m_name));
40 0 : }
41 :
42 0 : void NewKeyCommand::undo ()
43 : {
44 : // remove new node
45 0 : m_parentNode->getChildren ()->removeRow (m_parentNode->getChildIndexByName (m_name));
46 0 : }
47 :
48 0 : void NewKeyCommand::redo ()
49 : {
50 : // insert new node
51 0 : m_parentNode->getChildren ()->append (m_newNode);
52 0 : }
53 :
54 0 : QStringList NewKeyCommand::cutListAtIndex (QStringList & list, int index)
55 : {
56 0 : for (int i = 0; i < index; i++)
57 0 : list.removeFirst ();
58 :
59 0 : return list;
60 0 : }
|