【发布时间】:2017-12-05 05:34:15
【问题描述】:
我有一个名为 Grid 的类,它由 Cells 组成。每个单元格可以有自己的格式(概念类似于 MS Excel)。
Grid 中的格式保存在一个向量 std::vector<std::unique_ptr<CellFormat>> m_CellFormatTable 中,它拥有所有格式,所以每当我需要读取 Cells 格式时,我都会从向量中读取它,只要有变化,就会报告给向量。抱歉,我对 C++11 标准很陌生,所以我的想法可能是错误的。
由于网格是一个矩阵,当一个单元格的格式发生变化时,每个单元格都属于矩阵的不同部分,它应该反映在矩阵的正确部分,即在向量中正确定位(CellFormatTable) .所以现阶段我不能使用vector的push_back方法。
CellFormat 类:
struct CellFormat
{
wxFont m_Font;
wxColor m_BackgroundColor, m_TextColor;
int m_HorizontalAlignment, m_VerticalAlignment;
CellFormat(Grid* ws) {
m_BackgroundColor = ws->GetDefaultCellBackgroundColour();
m_TextColor=ws->GetDefaultCellTextColour();
int horizontal = 0, vertical = 0;
ws->GetDefaultCellAlignment(&horizontal, &vertical);
}
CellFormat(const CellFormat& other) {
m_Font = other.m_Font;
m_BackgroundColor = other.m_BackgroundColor;
m_TextColor = other.m_TextColor;
m_HorizontalAlignment = other.m_HorizontalAlignment;
m_VerticalAlignment = other.m_VerticalAlignment;
}
CellFormat& operator=(const CellFormat& other) {
if (this == &other) return *this;
m_Font = other.m_Font;
m_BackgroundColor = other.m_BackgroundColor;
m_TextColor = other.m_TextColor;
m_HorizontalAlignment = other.m_HorizontalAlignment;
m_VerticalAlignment = other.m_VerticalAlignment;
return *this;
}
};
在 Grid.h 中
class Grid{
std::vector<std::unique_ptr<CellFormat>> m_CellFormatTable;
//
CellFormat* GetCellFormat(int row, int column);
void SetCellFormat(int row, int column, CellFormat format);
void ApplyCellFormat(int row, int column, const CellFormat* format);
CellFormat* CreateCellFormat(int row, int column);
//rest is omitted
}
在 Grid.cpp 中
Grid(some arguments){
m_CellFormatTable.resize(nrows*ncols);
//rest is omitted
}
CellFormat* Grid::GetCellFormat(int row, int column)
{
int ncols= GetNumberCols();
return m_CellFormatTable[row*ncols+ column].get();
}
void Grid::SetCellFormat(int row, int column, CellFormat other)
{
CellFormat* format = GetCellFormat(row, column);
if (format == 0) format = CreateCellFormat(row, column);
*format = other;
}
void Grid::ApplyCellFormat(int row, int column, const CellFormat * format)
{
if (format == 0) {
int ncols= GetNumberCols();
//Set everything to default values
//Omitted
m_CellFormatTable[row*ncols+ column].reset();
}
else {
wxColor bgcolor = format->m_BackgroundColor;
if (bgcolor.IsOk()) SetCellBackgroundColour(row, column, bgcolor);
SetCellTextColour(row, column, format->m_TextColor);
SetCellFont(row, column, format->m_Font);
SetCellAlignment(row, column, format->m_HorizontalAlignment, format->m_VerticalAlignment);
}
}
CellFormat* Grid::CreateCellFormat(int row, int column)
{
int ncols= GetNumberCols();
CellFormat* format = new CellFormat(this);
m_CellFormatTable.emplace(m_CellFormatTable.begin() + row*ncols+ column, std::move(format));
return format;
}
每当我格式化一个单元格时,说它的背景颜色发生了变化,我都会使用以下尝试:
CellFormat* format = ws->GetCellFormat(row, col);
if (format == 0) format = ws->CreateCellFormat(row, col);
if (ChangeFillColor) {
ws->SetCellBackgroundColour(row, col, m_LastChosenFillColor);
format->m_BackgroundColor = m_LastChosenFillColor;
}
代码在format->m_BackgroundColor 处的ApplyCellFormat 函数处失败,因为应该是单元格背景颜色的颜色无效。这告诉我,CreateCellFormat 很可能没有将 CellFormat 放在正确的位置。我尝试使用 insert 而不是 emplace 但编译器(VS 2015)抱怨我所有的尝试。
任何想法表示赞赏。
【问题讨论】:
-
尝试将所有这些代码墙归结为您问题的本质。不清楚的是,矩阵是否应该是满的(即所有有效的对
i,j都被一个单元格占用)。 -
@macroland,你为什么使用自己的网格和表格类? wxWidgets 已经具备了所有这些功能,包括定制网格表的可能性......
-
请将解决方案发布为答案,而不是更新您的问题。这是为了帮助未来的访客并避免混淆。谢谢。