| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | #include "tableview.h"
 | 
					
						
							|  |  |  | #include "models.h"
 | 
					
						
							| 
									
										
										
										
											2013-12-11 21:44:05 -02:00
										 |  |  | #include "modeldelegates.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QPushButton>
 | 
					
						
							| 
									
										
										
										
											2013-09-26 20:35:30 -03:00
										 |  |  | #include <QLayout>
 | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | #include <QFile>
 | 
					
						
							|  |  |  | #include <QTextStream>
 | 
					
						
							|  |  |  | #include <QSettings>
 | 
					
						
							| 
									
										
										
										
											2014-10-17 21:22:49 +02:00
										 |  |  | #include <QStyle>
 | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-17 21:22:49 +02:00
										 |  |  | TableView::TableView(QWidget *parent) : QGroupBox(parent) | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	ui.setupUi(this); | 
					
						
							| 
									
										
										
										
											2014-01-15 18:52:42 +01:00
										 |  |  | 	ui.tableView->setItemDelegate(new DiveListDelegate(this)); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QFontMetrics fm(defaultModelFont()); | 
					
						
							|  |  |  | 	int text_ht = fm.height(); | 
					
						
							|  |  |  | 	int text_em = fm.width('m'); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	metrics.icon = &defaultIconMetrics(); | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	metrics.col_width = 7*text_em; | 
					
						
							| 
									
										
										
										
											2014-10-19 16:15:20 +02:00
										 |  |  | 	metrics.rm_col_width = metrics.icon->sz_small + 2*metrics.icon->spacing; | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 	metrics.header_ht = text_ht + 10; // TODO DPI
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-19 16:15:21 +02:00
										 |  |  | 	/* We want to get rid of the margin around the table, but
 | 
					
						
							|  |  |  | 	 * we must be careful with some styles (e.g. GTK+) where the top | 
					
						
							|  |  |  | 	 * margin is actually used to hold the label. We thus check the | 
					
						
							|  |  |  | 	 * rectangles for the label and contents to make sure they do not | 
					
						
							|  |  |  | 	 * overlap, and adjust the top contentsMargin accordingly | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// start by setting all the margins at zero
 | 
					
						
							|  |  |  | 	QMargins margins; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// grab the label and contents dimensions and positions
 | 
					
						
							|  |  |  | 	QStyleOptionGroupBox option; | 
					
						
							|  |  |  | 	initStyleOption(&option); | 
					
						
							|  |  |  | 	QRect labelRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this); | 
					
						
							|  |  |  | 	QRect contentsRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxContents, this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* we need to ensure that the bottom of the label is higher
 | 
					
						
							|  |  |  | 	 * than the top of the contents */ | 
					
						
							|  |  |  | 	int delta = contentsRect.top() - labelRect.bottom(); | 
					
						
							|  |  |  | 	const int min_gap = metrics.icon->spacing; | 
					
						
							|  |  |  | 	if (delta <= min_gap) { | 
					
						
							|  |  |  | 		margins.setTop(min_gap - delta); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	layout()->setContentsMargins(margins); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 	QIcon plusIcon(":plus"); | 
					
						
							| 
									
										
										
										
											2014-10-17 21:22:49 +02:00
										 |  |  | 	plusBtn = new QPushButton(plusIcon, QString(), this); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 	plusBtn->setFlat(true); | 
					
						
							| 
									
										
										
										
											2014-07-11 18:39:03 +01:00
										 |  |  | 	plusBtn->setToolTip(tr("Add cylinder")); | 
					
						
							| 
									
										
										
										
											2014-10-19 16:15:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* now determine the icon and button size. Since the button will be
 | 
					
						
							|  |  |  | 	 * placed in the label, make sure that we do not overflow, as it might | 
					
						
							|  |  |  | 	 * get clipped | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	int iconSize = metrics.icon->sz_small; | 
					
						
							|  |  |  | 	int btnSize = iconSize + 2*min_gap; | 
					
						
							|  |  |  | 	if (btnSize > labelRect.height()) { | 
					
						
							|  |  |  | 		btnSize = labelRect.height(); | 
					
						
							|  |  |  | 		iconSize = btnSize - 2*min_gap; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	plusBtn->setIconSize(QSize(iconSize, iconSize)); | 
					
						
							|  |  |  | 	plusBtn->resize(btnSize, btnSize); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 	connect(plusBtn, SIGNAL(clicked(bool)), this, SIGNAL(addButtonClicked())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TableView::~TableView() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup(objectName()); | 
					
						
							| 
									
										
										
										
											2014-08-26 17:52:05 -07:00
										 |  |  | 	// remove the old default
 | 
					
						
							|  |  |  | 	bool oldDefault = (ui.tableView->columnWidth(0) == 30); | 
					
						
							|  |  |  | 	for (int i = 1; oldDefault && i < ui.tableView->model()->columnCount(); i++) { | 
					
						
							|  |  |  | 		if (ui.tableView->columnWidth(i) != 80) | 
					
						
							|  |  |  | 			oldDefault = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (oldDefault) { | 
					
						
							|  |  |  | 		s.remove(""); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for (int i = 0; i < ui.tableView->model()->columnCount(); i++) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 			if (ui.tableView->columnWidth(i) == defaultColumnWidth(i)) | 
					
						
							| 
									
										
										
										
											2014-08-26 17:52:05 -07:00
										 |  |  | 				s.remove(QString("colwidth%1").arg(i)); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	s.endGroup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TableView::setBtnToolTip(const QString &tooltip) | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	plusBtn->setToolTip(tooltip); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | void TableView::setModel(QAbstractItemModel *model) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.tableView->setModel(model); | 
					
						
							|  |  |  | 	connect(ui.tableView, SIGNAL(clicked(QModelIndex)), model, SLOT(remove(QModelIndex))); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup(objectName()); | 
					
						
							| 
									
										
										
										
											2013-11-21 23:16:19 -02:00
										 |  |  | 	const int columnCount = ui.tableView->model()->columnCount(); | 
					
						
							|  |  |  | 	for (int i = 0; i < columnCount; i++) { | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 		QVariant width = s.value(QString("colwidth%1").arg(i), defaultColumnWidth(i)); | 
					
						
							| 
									
										
										
										
											2013-11-21 23:16:19 -02:00
										 |  |  | 		ui.tableView->setColumnWidth(i, width.toInt()); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	s.endGroup(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | 	ui.tableView->horizontalHeader()->setMinimumHeight(metrics.header_ht); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TableView::fixPlusPosition() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-10-17 21:22:49 +02:00
										 |  |  | 	QStyleOptionGroupBox option; | 
					
						
							|  |  |  | 	initStyleOption(&option); | 
					
						
							|  |  |  | 	QRect labelRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::SC_GroupBoxLabel, this); | 
					
						
							|  |  |  | 	QRect contentsRect = style()->subControlRect(QStyle::CC_GroupBox, &option, QStyle::QStyle::SC_GroupBoxFrame, this); | 
					
						
							|  |  |  | 	plusBtn->setGeometry( contentsRect.width() - plusBtn->width(), labelRect.y(), plusBtn->width(), labelRect.height()); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // We need to manually position the 'plus' on cylinder and weight.
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TableView::resizeEvent(QResizeEvent *event) | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	fixPlusPosition(); | 
					
						
							|  |  |  | 	QWidget::resizeEvent(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TableView::showEvent(QShowEvent *event) | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	QWidget::showEvent(event); | 
					
						
							|  |  |  | 	fixPlusPosition(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void TableView::edit(const QModelIndex &index) | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.tableView->edit(index); | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-15 15:30:48 +02:00
										 |  |  | int TableView::defaultColumnWidth(int col) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return col == CylindersModel::REMOVE ? metrics.rm_col_width : metrics.col_width; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | QTableView *TableView::view() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	return ui.tableView; | 
					
						
							| 
									
										
										
										
											2013-09-03 13:57:38 -03:00
										 |  |  | } |