ui: Separated QtCodeSnippet and added QtCodeFile to display filename
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
add_files(
|
||||
APP_FILES
|
||||
|
||||
qt/element/QtCodeFile.cpp
|
||||
qt/element/QtCodeFile.h
|
||||
qt/element/QtCodeSnippet.cpp
|
||||
qt/element/QtCodeSnippet.h
|
||||
qt/element/QtMainWindow.cpp
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "qt/element/QtCodeFile.h"
|
||||
|
||||
#include <QLabel>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include "qt/element/QtCodeSnippet.h"
|
||||
|
||||
QtCodeFile::QtCodeFile(QtCodeView* parentView, const std::string& fileName, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_parentView(parentView)
|
||||
, m_fileName(fileName)
|
||||
{
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
layout->setMargin(0);
|
||||
layout->setSpacing(2);
|
||||
setLayout(layout);
|
||||
|
||||
QLabel* label = new QLabel(fileName.c_str(), this);
|
||||
QFontMetrics metrics(label->font());
|
||||
|
||||
label->setStyleSheet("background-color: #E1E1E1; padding: 3px;");
|
||||
label->setFixedWidth(metrics.boundingRect(fileName.c_str()).width() + 12);
|
||||
layout->addWidget(label);
|
||||
}
|
||||
|
||||
QtCodeFile::~QtCodeFile()
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& QtCodeFile::getFileName() const
|
||||
{
|
||||
return m_fileName;
|
||||
}
|
||||
|
||||
void QtCodeFile::addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber)
|
||||
{
|
||||
std::shared_ptr<QtCodeSnippet> snippet =
|
||||
std::make_shared<QtCodeSnippet>(m_parentView, str, locationFile, startLineNumber, this);
|
||||
layout()->addWidget(snippet.get());
|
||||
m_snippets.push_back(snippet);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef QT_CODE_FILE_H
|
||||
#define QT_CODE_FILE_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QtCodeSnippet;
|
||||
class QtCodeView;
|
||||
class TokenLocationFile;
|
||||
|
||||
class QtCodeFile : public QWidget
|
||||
{
|
||||
public:
|
||||
QtCodeFile(QtCodeView* parentView, const std::string& fileName, QWidget *parent);
|
||||
virtual ~QtCodeFile();
|
||||
|
||||
const std::string& getFileName() const;
|
||||
|
||||
void addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
|
||||
|
||||
private:
|
||||
QtCodeView* m_parentView;
|
||||
std::vector<std::shared_ptr<QtCodeSnippet> > m_snippets;
|
||||
const std::string m_fileName;
|
||||
};
|
||||
|
||||
#endif // QT_CODE_FILE_H
|
||||
@@ -45,6 +45,7 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
m_lineNumberArea = new LineNumberArea(this);
|
||||
|
||||
setReadOnly(true);
|
||||
setFrameStyle(QFrame::NoFrame);
|
||||
|
||||
QFont font;
|
||||
font.setFamily(ApplicationSettings::getInstance()->getCodeFontName().c_str());
|
||||
@@ -88,7 +89,7 @@ void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QString number = QString::number(blockNumber + m_startLineNumber);
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawText(0, top, m_lineNumberArea->width() - 1, fontMetrics().height(), Qt::AlignRight, number);
|
||||
painter.drawText(0, top, m_lineNumberArea->width() - 3, fontMetrics().height(), Qt::AlignRight, number);
|
||||
}
|
||||
|
||||
block = block.next();
|
||||
@@ -109,7 +110,7 @@ int QtCodeSnippet::lineNumberAreaWidth()
|
||||
digits++;
|
||||
}
|
||||
|
||||
int width = 2 + fontMetrics().width(QLatin1Char('9')) * digits;
|
||||
int width = fontMetrics().width(QLatin1Char('9')) * digits + 6;
|
||||
return width;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "data/location/TokenLocationFile.h"
|
||||
#include "qt/element/QtCodeSnippet.h"
|
||||
#include "qt/element/QtCodeFile.h"
|
||||
#include "qt/QtWidgetWrapper.h"
|
||||
#include "qt/utility/utilityQt.h"
|
||||
#include "utility/FileSystem.h"
|
||||
#include "utility/messaging/type/MessageActivateToken.h"
|
||||
|
||||
QtCodeView::QtCodeView(ViewLayout* viewLayout)
|
||||
@@ -54,15 +55,32 @@ void QtCodeView::activateToken(Id tokenId) const
|
||||
|
||||
void QtCodeView::doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber)
|
||||
{
|
||||
std::shared_ptr<QtCodeSnippet> snippet = std::make_shared<QtCodeSnippet>(this, str, locationFile, startLineNumber);
|
||||
std::string fileName = FileSystem::fileName(locationFile.getFilePath());
|
||||
QtCodeFile* file = nullptr;
|
||||
|
||||
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
|
||||
widget->layout()->addWidget(snippet.get());
|
||||
for (std::shared_ptr<QtCodeFile> filePtr : m_files)
|
||||
{
|
||||
if (filePtr->getFileName() == fileName)
|
||||
{
|
||||
file = filePtr.get();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_snippets.push_back(snippet);
|
||||
if (!file)
|
||||
{
|
||||
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
|
||||
std::shared_ptr<QtCodeFile> filePtr = std::make_shared<QtCodeFile>(this, fileName, widget);
|
||||
|
||||
m_files.push_back(filePtr);
|
||||
file = filePtr.get();
|
||||
widget->layout()->addWidget(file);
|
||||
}
|
||||
|
||||
file->addCodeSnippet(str, locationFile, startLineNumber);
|
||||
}
|
||||
|
||||
void QtCodeView::doClearCodeSnippets()
|
||||
{
|
||||
m_snippets.clear();
|
||||
m_files.clear();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "qt/utility/QtThreadedFunctor.h"
|
||||
#include "utility/types.h"
|
||||
|
||||
class QtCodeSnippet;
|
||||
class QtCodeFile;
|
||||
|
||||
class QtCodeView: public CodeView
|
||||
{
|
||||
@@ -30,7 +30,7 @@ private:
|
||||
void doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
|
||||
void doClearCodeSnippets();
|
||||
|
||||
std::vector<std::shared_ptr<QtCodeSnippet> > m_snippets;
|
||||
std::vector<std::shared_ptr<QtCodeFile> > m_files;
|
||||
|
||||
QtThreadedFunctor<void> m_clearCodeSnippetsFunctor;
|
||||
QtThreadedFunctor<const std::string&, const TokenLocationFile&, int> m_addCodeSnippetFunctor;
|
||||
|
||||
@@ -44,3 +44,7 @@ bool FileSystem::exists(const std::string& path)
|
||||
return boost::filesystem::exists(boost::filesystem::path(path));
|
||||
}
|
||||
|
||||
std::string FileSystem::fileName(const std::string& path)
|
||||
{
|
||||
return boost::filesystem::path(path).filename().generic_string();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ public:
|
||||
const std::string& path, const std::vector<std::string>& extensions
|
||||
);
|
||||
static bool exists(const std::string& path);
|
||||
static std::string fileName(const std::string& path);
|
||||
|
||||
private:
|
||||
static bool isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions);
|
||||
};
|
||||
|
||||
@@ -60,6 +60,12 @@ public:
|
||||
TS_ASSERT(!FileSystem::exists("data/FileSystemTestSuite/blabla.h"));
|
||||
}
|
||||
|
||||
void test_filesystem_extracts_filename()
|
||||
{
|
||||
TS_ASSERT_EQUALS(FileSystem::fileName("data/FileSystemTestSuite/tictactoe.h"), "tictactoe.h");
|
||||
TS_ASSERT_EQUALS(FileSystem::fileName("data/FileSystemTestSuite/Settings/player.h"), "player.h");
|
||||
}
|
||||
|
||||
private:
|
||||
bool isInVector(const std::vector<std::string>& files, const std::string filename)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user