File: OrderedListEditor.cpp

package info (click to toggle)
renderdoc 1.27%2Bdfsg-1
  • links: PTS, VCS
  • area: non-free
  • in suites: sid
  • size: 107,796 kB
  • sloc: cpp: 763,519; ansic: 326,847; python: 26,946; xml: 23,189; java: 11,382; cs: 7,181; makefile: 6,707; yacc: 5,682; ruby: 4,648; perl: 3,461; sh: 2,381; php: 2,119; lisp: 1,835; javascript: 1,525; tcl: 1,068; ml: 747
file content (213 lines) | stat: -rw-r--r-- 6,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2019-2023 Baldur Karlsson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

#include "OrderedListEditor.h"
#include <QHeaderView>
#include <QKeyEvent>
#include <QToolButton>
#include "Code/QRDUtils.h"
#include "Code/Resources.h"

OrderedListEditor::OrderedListEditor(const QString &itemName, BrowseMode browse, QWidget *parent)
    : RDTableWidget(parent)
{
  setFont(Formatter::PreferredFont());

  m_BrowseMode = browse;

  setDragEnabled(true);
  setDragDropOverwriteMode(false);
  setDragDropMode(QAbstractItemView::InternalMove);
  setDefaultDropAction(Qt::MoveAction);
  setAlternatingRowColors(true);
  setSelectionMode(QAbstractItemView::SingleSelection);
  setSelectionBehavior(QAbstractItemView::SelectRows);
  setCornerButtonEnabled(false);

  horizontalHeader()->setHighlightSections(false);
  horizontalHeader()->setMinimumSectionSize(50);
  verticalHeader()->setHighlightSections(false);

  if(m_BrowseMode == BrowseMode::None)
  {
    setColumnCount(1);
    setHorizontalHeaderLabels({itemName});
    horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
  }
  else
  {
    QStringList labels;
    labels << itemName << tr("Browse");
    setColumnCount(2);
    setHorizontalHeaderLabels(labels);

    horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
    horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
  }

  QObject::connect(this, &RDTableWidget::cellChanged, this, &OrderedListEditor::cellChanged);
}

OrderedListEditor::~OrderedListEditor()
{
}

QToolButton *OrderedListEditor::makeBrowseButton()
{
  QToolButton *ret = new QToolButton(this);

  ret->setIcon(Icons::folder_page_white());
  ret->setAutoRaise(true);

  QObject::connect(ret, &QToolButton::clicked, this, &OrderedListEditor::browse);

  return ret;
}

void OrderedListEditor::setItems(const QStringList &strings)
{
  setUpdatesEnabled(false);
  clearContents();

  setRowCount(strings.count());

  for(int i = 0; i < strings.count(); i++)
  {
    setItem(i, 0, new QTableWidgetItem(strings[i]));

    if(m_BrowseMode != BrowseMode::None)
      setCellWidget(i, 1, makeBrowseButton());
  }

  // if we added any strings above the new item row was automatically
  // appended. If not, add it explicitly here
  if(strings.count() == 0)
    addNewItemRow();

  resizeColumnToContents(0);
  if(m_BrowseMode != BrowseMode::None)
    resizeColumnToContents(1);

  setUpdatesEnabled(true);
}

void OrderedListEditor::addNewItemRow()
{
  insertRow(rowCount());

  QTableWidgetItem *item = new QTableWidgetItem(QString());
  item->setFlags(item->flags() & ~(Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled));
  setItem(rowCount() - 1, 0, item);

  if(m_BrowseMode != BrowseMode::None)
  {
    item = new QTableWidgetItem(QString());
    item->setFlags(item->flags() & ~(Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled));
    setItem(rowCount() - 1, 1, item);

    setCellWidget(rowCount() - 1, 1, makeBrowseButton());
  }
}

QStringList OrderedListEditor::getItems()
{
  QStringList ret;

  // don't include the last 'new item' entry
  for(int i = 0; i < rowCount() - 1; i++)
    ret << item(i, 0)->text();

  return ret;
}

void OrderedListEditor::cellChanged(int row, int column)
{
  // hack :(. Assume this will only be hit on single UI thread.
  static bool recurse = false;

  if(recurse)
    return;

  recurse = true;

  // if the last row has something added to it, make a new final row
  if(row == rowCount() - 1)
  {
    if(!item(row, column)->data(Qt::DisplayRole).toString().trimmed().isEmpty())
    {
      // enable dragging
      item(row, 0)->setFlags(item(row, 0)->flags() | (Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled));
      if(m_BrowseMode != BrowseMode::None)
        delete takeItem(row, 1);

      addNewItemRow();
    }
  }

  if(row > 0 && column == 0 && item(row, column)->data(Qt::DisplayRole).toString().trimmed().isEmpty())
  {
    removeRow(row);
  }

  recurse = false;
}

void OrderedListEditor::browse()
{
  QWidget *tool = qobject_cast<QWidget *>(QObject::sender());

  if(tool)
  {
    for(int i = 0; i < rowCount(); i++)
    {
      QWidget *rowButton = cellWidget(i, 1);
      if(rowButton == tool)
      {
        QString sel;
        if(m_BrowseMode == BrowseMode::Folder)
          sel = RDDialog::getExistingDirectory(this, tr("Browse for a folder"));
        else
          sel = RDDialog::getOpenFileName(this, tr("Browse for a file"));

        if(!sel.isEmpty())
          item(i, 0)->setText(sel);
      }
    }
  }
}

void OrderedListEditor::keyPressEvent(QKeyEvent *event)
{
  if(event->key() == Qt::Key_Delete)
  {
    int row = -1;
    if(selectionModel()->selectedIndexes().count() > 0)
      row = selectionModel()->selectedIndexes()[0].row();

    if(row >= 0)
      removeRow(row);
  }

  RDTableWidget::keyPress(event);
}