SMILX  1.01
milxQtManager.cpp
1 /*=========================================================================
2  The Software is copyright (c) Commonwealth Scientific and Industrial Research Organisation (CSIRO)
3  ABN 41 687 119 230.
4  All rights reserved.
5 
6  Licensed under the CSIRO BSD 3-Clause License
7  You may not use this file except in compliance with the License.
8  You may obtain a copy of the License in the file LICENSE.md or at
9 
10  https://stash.csiro.au/projects/SMILI/repos/smili/browse/license.txt
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 =========================================================================*/
18 #include "milxQtManager.h"
19 
20 milxQtManager::milxQtManager(QWidget *theParent) : QTabWidget(theParent)
21 {
22  setTabsClosable(true);
23  setObjectName("Manager");
24 
25 // newTab();
26 
27  createActions();
29 }
30 
31 milxQtManager::~milxQtManager()
32 {
33  //dtor
34 }
35 
36 int milxQtManager::newTab(QString tabTitle, QStringList headings)
37 {
38  QTreeWidget *treeWidget = new QTreeWidget(this);
39 
40  treeWidget->setColumnCount(headings.size());
41  treeWidget->setHeaderLabels(headings);
42 
43  int newIndex = addTab(treeWidget, tabTitle);
44  setCurrentIndex(newIndex);
45  return newIndex;
46 }
47 
48 void milxQtManager::closeTab(int index)
49 {
50  int newIndex = 0;
51 
52  if(count() > 1 || index > 0)
53  {
54  removeTab(index);
55 
56  if(index > 0 && currentIndex() == index)
57  newIndex = currentIndex();
58  else if(index > 0)
59  newIndex = index-1; //Safe to decrement
60  setCurrentIndex(newIndex); //else use zero
61  }
62 }
63 
65 {
66  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( currentWidget() );
67 
68  if(!currentTree)
69  return;
70 
71  currentTree->clear();
72 }
73 
74 void milxQtManager::clearTab(int tabIndex)
75 {
76  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( widget(tabIndex) );
77 
78  if(!currentTree)
79  return;
80 
81  currentTree->clear();
82 }
83 
84 void milxQtManager::exportTab(QString filename)
85 {
86  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( currentWidget() );
87 
88  if(!currentTree)
89  return;
90 
91  QPointer<QFileDialog> fileOpener = new QFileDialog;
92  QSettings settings("Shekhar Chandra", "milxQt");
93  QString path = settings.value("recentPath").toString();
94 
95  if(filename.isEmpty())
96  {
97  QFileDialog *fileOpener = new QFileDialog;
98  filename = fileOpener->getSaveFileName(this,
99  tr("Select File to Save"),
100  path,
101  tr(openOtherExts.c_str()) );
102  }
103 
104  if(filename.isEmpty())
105  return;
106 
107  QFile txtFile;
108  txtFile.setFileName(filename);
109  if(!txtFile.open(QIODevice::WriteOnly | QIODevice::Text))
110  {
111  milx::PrintError("Could not open text file for export.");
112  return;
113  }
114 
115  QTextStream outFile(&txtFile);
116  QTreeWidgetItemIterator itemIterator(currentTree);
117  while (*itemIterator)
118  {
119  QTreeWidgetItem *item = *itemIterator;
120  for(int j = 0; j < item->columnCount(); j ++)
121  {
122  outFile << item->text(j);
123  if(j < item->columnCount()-1)
124  outFile << ", ";
125  }
126  outFile << endl;
127  itemIterator ++;
128  }
129  txtFile.close();
130 }
131 
132 void milxQtManager::addItem(QStringList entries, Qt::ItemFlags flags)
133 {
134  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( currentWidget() );
135  QTreeWidgetItem *newEntry = new QTreeWidgetItem(currentTree);
136 
137  for(int j = 0; j < entries.size(); j ++)
138  newEntry->setText(j, entries[j]);
139  newEntry->setFlags(flags);
140  currentTree->itemBelow(newEntry);
141 }
142 
143 void milxQtManager::addItem(int tabIndex, QStringList entries, Qt::ItemFlags flags)
144 {
145  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( widget(tabIndex) );
146  QTreeWidgetItem *newEntry = new QTreeWidgetItem(currentTree);
147 
148  for(int j = 0; j < entries.size(); j ++)
149  newEntry->setText(j, entries[j]);
150  newEntry->setFlags(flags);
151 
152  currentTree->itemBelow(newEntry);
153 }
154 
155 void milxQtManager::addItem(int tabIndex, QStringList entries, QWidget *itemWidgetToAdd, int widgetColumn)
156 {
157  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>( widget(tabIndex) );
158  QTreeWidgetItem *newEntry = new QTreeWidgetItem(currentTree);
159 
160  for(int j = 0; j < entries.size(); j ++)
161  newEntry->setText(j, entries[j]);
162 
163  currentTree->setItemWidget(newEntry, widgetColumn, itemWidgetToAdd);
164  currentTree->itemBelow(newEntry);
165 }
166 
167 void milxQtManager::addTreeItem(int tabIndex, QStringList topLevelName, QList<QStringList> entryList, Qt::ItemFlags flags)
168 {
169  QTreeWidget *currentTree = qobject_cast<QTreeWidget *>(widget(tabIndex));
170  //QList<QTreeWidgetItem *> items;
171 
172  //Add top level item first
173  QTreeWidgetItem *topLevelEntry = new QTreeWidgetItem(currentTree, topLevelName);
174  currentTree->addTopLevelItem(topLevelEntry);
175 
176  //Add leaves
177  for (int j = 0; j < entryList.size(); j++)
178  {
179  QTreeWidgetItem *newEntry = new QTreeWidgetItem(topLevelEntry, entryList[j]);
180  newEntry->setFlags(flags);
181  //items.append(newEntry);
182  currentTree->itemBelow(newEntry);
183  }
184 
185  //currentTree->setCurrentItem(0, items);
186 }
187 
189 {
190  actionExportTab = new QAction(this);
191  actionExportTab->setText(QApplication::translate("Manager", "Export Contents ...", 0, QApplication::UnicodeUTF8));
192  actionExportTab->setShortcut(tr("Alt+e"));
193  actionClearTab = new QAction(this);
194  actionClearTab->setText(QApplication::translate("Manager", "Clear Tab", 0, QApplication::UnicodeUTF8));
195  actionClearTab->setShortcut(tr("Alt+t"));
196  actionClear = new QAction(this);
197  actionClear->setText(QApplication::translate("Manager", "Clear Manager", 0, QApplication::UnicodeUTF8));
198  actionClear->setShortcut(tr("Alt+c"));
199 }
200 
202 {
203  connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
204 
205  connect(actionExportTab, SIGNAL(triggered()), this, SLOT(exportTab()));
206  connect(actionClearTab, SIGNAL(triggered()), this, SLOT(clearTab()));
207  connect(actionClear, SIGNAL(triggered()), this, SLOT(clear()));
208 }
209 
210 void milxQtManager::contextMenuEvent(QContextMenuEvent *currentEvent)
211 {
212  QMenu* contextMenu = new QMenu(this);
213 
214  contextMenu->addAction(actionExportTab);
215  contextMenu->addAction(actionClearTab);
216  contextMenu->addAction(actionClear);
217 
218  contextMenu->exec(currentEvent->globalPos());
219 }
QAction * actionClearTab
clear tab action
Definition: milxQtManager.h:98
QAction * actionExportTab
export tab action
Definition: milxQtManager.h:97
void PrintError(const std::string msg)
Displays a generic msg to standard error with carriage return.
Definition: milxGlobal.h:202
void closeTab(int index)
Close the current tab.
int newTab(QString tabTitle, QStringList headings)
Creates a new tab in the manager with the title and headings provided.
QAction * actionClear
clear manager action
Definition: milxQtManager.h:99
void addItem(QStringList entries, Qt::ItemFlags flags=Qt::ItemIsEnabled|Qt::ItemIsSelectable)
Add all the entries in list to the view.
void addTreeItem(int tabIndex, QStringList topLevelName, QList< QStringList > entryList, Qt::ItemFlags flags=Qt::ItemIsEnabled|Qt::ItemIsSelectable)
Add all the entries in list to the view like a tree.
void exportTab(QString filename="")
Clear the tab given by index.
void createConnections()
Create the connections for context menu etc.
void contextMenuEvent(QContextMenuEvent *event)
The context menu setup member.
milxQtManager(QWidget *theParent=0)
The standard constructor.
void createActions()
Create the actions for context menu etc.
void clearTab()
Clear the current tab.