mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 03:40:05 +00:00 
			
		
		
		
	Alert model cleanup, and size hints for columns
This commit is contained in:
		
							parent
							
								
									d19656f0f1
								
							
						
					
					
						commit
						dabcf1114e
					
				
					 3 changed files with 77 additions and 30 deletions
				
			
		|  | @ -10,6 +10,8 @@ | ||||||
| #include <format> | #include <format> | ||||||
| 
 | 
 | ||||||
| #include <GeographicLib/Geodesic.hpp> | #include <GeographicLib/Geodesic.hpp> | ||||||
|  | #include <QApplication> | ||||||
|  | #include <QFontMetrics> | ||||||
| 
 | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
| { | { | ||||||
|  | @ -24,7 +26,7 @@ static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||||
| static constexpr int kFirstColumn = static_cast<int>(AlertModel::Column::Etn); | static constexpr int kFirstColumn = static_cast<int>(AlertModel::Column::Etn); | ||||||
| static constexpr int kLastColumn = | static constexpr int kLastColumn = | ||||||
|    static_cast<int>(AlertModel::Column::Distance); |    static_cast<int>(AlertModel::Column::Distance); | ||||||
| static const int kNumColumns = 9; | static constexpr int kNumColumns = kLastColumn - kFirstColumn + 1; | ||||||
| 
 | 
 | ||||||
| class AlertModelImpl | class AlertModelImpl | ||||||
| { | { | ||||||
|  | @ -96,15 +98,17 @@ int AlertModel::columnCount(const QModelIndex& parent) const | ||||||
| 
 | 
 | ||||||
| QVariant AlertModel::data(const QModelIndex& index, int role) const | QVariant AlertModel::data(const QModelIndex& index, int role) const | ||||||
| { | { | ||||||
|    if (index.isValid() && index.row() >= 0 && |    if (!index.isValid() || index.row() < 0 || | ||||||
|        index.row() < p->textEventKeys_.size() && |        index.row() >= p->textEventKeys_.size()) | ||||||
|        (role == Qt::DisplayRole || role == types::SortRole || |  | ||||||
|         (role == types::TimePointRole && |  | ||||||
|          (index.column() == static_cast<int>(Column::StartTime) || |  | ||||||
|           index.column() == static_cast<int>(Column::EndTime))))) |  | ||||||
|    { |    { | ||||||
|  |       return QVariant(); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|    const auto& textEventKey = p->textEventKeys_.at(index.row()); |    const auto& textEventKey = p->textEventKeys_.at(index.row()); | ||||||
| 
 | 
 | ||||||
|  |    if (role == Qt::ItemDataRole::DisplayRole || | ||||||
|  |        role == types::ItemDataRole::SortRole) | ||||||
|  |    { | ||||||
|       switch (index.column()) |       switch (index.column()) | ||||||
|       { |       { | ||||||
|       case static_cast<int>(Column::Etn): |       case static_cast<int>(Column::Etn): | ||||||
|  | @ -129,28 +133,12 @@ QVariant AlertModel::data(const QModelIndex& index, int role) const | ||||||
|             AlertModelImpl::GetCounties(textEventKey)); |             AlertModelImpl::GetCounties(textEventKey)); | ||||||
| 
 | 
 | ||||||
|       case static_cast<int>(Column::StartTime): |       case static_cast<int>(Column::StartTime): | ||||||
|          if (role == types::TimePointRole) |  | ||||||
|          { |  | ||||||
|             return QVariant::fromValue( |  | ||||||
|                AlertModelImpl::GetStartTime(textEventKey)); |  | ||||||
|          } |  | ||||||
|          else |  | ||||||
|          { |  | ||||||
|          return QString::fromStdString( |          return QString::fromStdString( | ||||||
|             AlertModelImpl::GetStartTimeString(textEventKey)); |             AlertModelImpl::GetStartTimeString(textEventKey)); | ||||||
|          } |  | ||||||
| 
 | 
 | ||||||
|       case static_cast<int>(Column::EndTime): |       case static_cast<int>(Column::EndTime): | ||||||
|          if (role == types::TimePointRole) |  | ||||||
|          { |  | ||||||
|             return QVariant::fromValue( |  | ||||||
|                AlertModelImpl::GetEndTime(textEventKey)); |  | ||||||
|          } |  | ||||||
|          else |  | ||||||
|          { |  | ||||||
|          return QString::fromStdString( |          return QString::fromStdString( | ||||||
|             AlertModelImpl::GetEndTimeString(textEventKey)); |             AlertModelImpl::GetEndTimeString(textEventKey)); | ||||||
|          } |  | ||||||
| 
 | 
 | ||||||
|       case static_cast<int>(Column::Distance): |       case static_cast<int>(Column::Distance): | ||||||
|          if (role == Qt::DisplayRole) |          if (role == Qt::DisplayRole) | ||||||
|  | @ -177,6 +165,20 @@ QVariant AlertModel::data(const QModelIndex& index, int role) const | ||||||
|          break; |          break; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  |    else if (role == types::ItemDataRole::TimePointRole) | ||||||
|  |    { | ||||||
|  |       switch (index.column()) | ||||||
|  |       { | ||||||
|  |       case static_cast<int>(Column::StartTime): | ||||||
|  |          return QVariant::fromValue(AlertModelImpl::GetStartTime(textEventKey)); | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::EndTime): | ||||||
|  |          return QVariant::fromValue(AlertModelImpl::GetEndTime(textEventKey)); | ||||||
|  | 
 | ||||||
|  |       default: | ||||||
|  |          break; | ||||||
|  |       } | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    return QVariant(); |    return QVariant(); | ||||||
| } | } | ||||||
|  | @ -222,6 +224,51 @@ AlertModel::headerData(int section, Qt::Orientation orientation, int role) const | ||||||
|          } |          } | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  |    else if (role == Qt::ItemDataRole::SizeHintRole) | ||||||
|  |    { | ||||||
|  |       static const QFontMetrics fontMetrics(QApplication::font()); | ||||||
|  | 
 | ||||||
|  |       QSize contentsSize {}; | ||||||
|  | 
 | ||||||
|  |       switch (section) | ||||||
|  |       { | ||||||
|  |       case static_cast<int>(Column::Etn): | ||||||
|  |          contentsSize = fontMetrics.size(0, "0000"); // 24x16
 | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::Phenomenon): | ||||||
|  |          contentsSize = fontMetrics.size(0, QString(10, 'W')); | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::State): | ||||||
|  |          contentsSize = fontMetrics.size(0, "WW, WW"); | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::Counties): | ||||||
|  |          contentsSize = fontMetrics.size(0, QString(15, 'W')); | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::StartTime): | ||||||
|  |       case static_cast<int>(Column::EndTime): | ||||||
|  |          contentsSize = fontMetrics.size(0, "0000-00-00 00:00:00"); | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       case static_cast<int>(Column::Distance): | ||||||
|  |          contentsSize = fontMetrics.size(0, "00000 km"); | ||||||
|  |          break; | ||||||
|  | 
 | ||||||
|  |       default: | ||||||
|  |          break; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       if (contentsSize != QSize {}) | ||||||
|  |       { | ||||||
|  |          // Add padding
 | ||||||
|  |          // TODO: Ideally, derived using the current style, but how?
 | ||||||
|  |          contentsSize += QSize(12, 8); | ||||||
|  |          return QVariant(contentsSize); | ||||||
|  |       } | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    return QVariant(); |    return QVariant(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -2,7 +2,6 @@ | ||||||
| 
 | 
 | ||||||
| #include <scwx/qt/types/text_event_key.hpp> | #include <scwx/qt/types/text_event_key.hpp> | ||||||
| #include <scwx/common/geographic.hpp> | #include <scwx/common/geographic.hpp> | ||||||
| #include <scwx/util/iterator.hpp> |  | ||||||
| 
 | 
 | ||||||
| #include <memory> | #include <memory> | ||||||
| 
 | 
 | ||||||
|  | @ -32,7 +31,6 @@ public: | ||||||
|       EndTime      = 7, |       EndTime      = 7, | ||||||
|       Distance     = 8 |       Distance     = 8 | ||||||
|    }; |    }; | ||||||
|    typedef util::Iterator<Column, Column::Etn, Column::Distance> ColumnIterator; |  | ||||||
| 
 | 
 | ||||||
|    explicit AlertModel(QObject* parent = nullptr); |    explicit AlertModel(QObject* parent = nullptr); | ||||||
|    ~AlertModel(); |    ~AlertModel(); | ||||||
|  |  | ||||||
|  | @ -67,6 +67,8 @@ AlertDockWidget::AlertDockWidget(QWidget* parent) : | ||||||
|    ui->alertView->header()->setSortIndicator( |    ui->alertView->header()->setSortIndicator( | ||||||
|       static_cast<int>(model::AlertModel::Column::Distance), |       static_cast<int>(model::AlertModel::Column::Distance), | ||||||
|       Qt::AscendingOrder); |       Qt::AscendingOrder); | ||||||
|  |    ui->alertView->header()->resizeSections( | ||||||
|  |       QHeaderView::ResizeMode::ResizeToContents); | ||||||
| 
 | 
 | ||||||
|    ui->alertSettings->addAction(ui->actionActiveAlerts); |    ui->alertSettings->addAction(ui->actionActiveAlerts); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat