Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Sep 12, 2023
1 parent 02bb22b commit a6017b3
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 32 deletions.
13 changes: 13 additions & 0 deletions lib/tlCore/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <tlCore/Error.h>
#include <tlCore/String.h>
#include <tlCore/StringFormat.h>

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -136,6 +137,18 @@ namespace tl
_sequence = value;
}

std::string Path::getSequenceString() const
{
std::string out;
if (isSequence())
{
out = string::Format("{0}-{1}").
arg(_sequence.getMin(), _padding, '0').
arg(_sequence.getMax(), _padding, '0');
}
return out;
}

bool Path::isEmpty() const
{
return
Expand Down
7 changes: 5 additions & 2 deletions lib/tlCore/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ namespace tl
//! Get the number.
const std::string& getNumber() const;

//! Get the number zero padding.
uint8_t getPadding() const;

//! Get the number sequence.
const math::IntRange& getSequence() const;

Expand All @@ -78,8 +81,8 @@ namespace tl
//! Get whether the given path is part of this sequence.
bool sequence(const Path&) const;

//! Get the number zero padding.
uint8_t getPadding() const;
//! Get the sequence string.
std::string getSequenceString() const;

//! Get the extension.
const std::string& getExtension() const;
Expand Down
10 changes: 5 additions & 5 deletions lib/tlCore/PathInline.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ namespace tl
return _number;
}

inline uint8_t Path::getPadding() const
{
return _padding;
}

inline const math::IntRange& Path::getSequence() const
{
return _sequence;
Expand All @@ -57,11 +62,6 @@ namespace tl
_extension == value._extension;
}

inline uint8_t Path::getPadding() const
{
return _padding;
}

inline const std::string& Path::getExtension() const
{
return _extension;
Expand Down
28 changes: 14 additions & 14 deletions lib/tlCore/StringFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,62 +86,62 @@ namespace tl
return *this;
}

Format& Format::arg(int value, int width)
Format& Format::arg(int value, int width, char pad)
{
std::stringstream ss;
ss << std::setw(width) << value;
ss << std::setfill(pad) << std::setw(width) << value;
return arg(ss.str());
}

Format& Format::arg(int8_t value, int width)
Format& Format::arg(int8_t value, int width, char pad)
{
std::stringstream ss;
ss << std::setw(width) << static_cast<int>(value);
ss << std::setfill(pad) << std::setw(width) << static_cast<int>(value);
return arg(ss.str());
}

Format& Format::arg(uint8_t value, int width)
Format& Format::arg(uint8_t value, int width, char pad)
{
std::stringstream ss;
ss << std::setw(width) << static_cast<int>(value);
ss << std::setfill(pad) << std::setw(width) << static_cast<int>(value);
return arg(ss.str());
}

Format& Format::arg(int16_t value, int width)
Format& Format::arg(int16_t value, int width, char pad)
{
std::stringstream ss;
ss << std::setw(width) << static_cast<int>(value);
ss << std::setfill(pad) << std::setw(width) << static_cast<int>(value);
return arg(ss.str());
}

Format& Format::arg(uint16_t value, int width)
Format& Format::arg(uint16_t value, int width, char pad)
{
std::stringstream ss;
ss << std::setw(width) << static_cast<int>(value);
ss << std::setfill(pad) << std::setw(width) << static_cast<int>(value);
return arg(ss.str());
}

Format& Format::arg(float value, int precision, int width)
Format& Format::arg(float value, int precision, int width, char pad)
{
std::stringstream ss;
if (precision >= 0)
{
ss.precision(precision);
ss << std::fixed;
}
ss << std::setw(width) << value;
ss << std::setfill(pad) << std::setw(width) << value;
return arg(ss.str());
}

Format& Format::arg(double value, int precision, int width)
Format& Format::arg(double value, int precision, int width, char pad)
{
std::stringstream ss;
if (precision >= 0)
{
ss.precision(precision);
ss << std::fixed;
}
ss << std::setw(width) << value;
ss << std::setfill(pad) << std::setw(width) << value;
return arg(ss.str());
}

Expand Down
14 changes: 7 additions & 7 deletions lib/tlCore/StringFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ namespace tl
///@{

Format& arg(const std::string&);
Format& arg(int, int width = 0);
Format& arg(int8_t, int width = 0);
Format& arg(uint8_t, int width = 0);
Format& arg(int16_t, int width = 0);
Format& arg(uint16_t, int width = 0);
Format& arg(float, int precision = -1, int width = 0);
Format& arg(double, int precision = -1, int width = 0);
Format& arg(int, int width = 0, char pad = ' ');
Format& arg(int8_t, int width = 0, char pad = ' ');
Format& arg(uint8_t, int width = 0, char pad = ' ');
Format& arg(int16_t, int width = 0, char pad = ' ');
Format& arg(uint16_t, int width = 0, char pad = ' ');
Format& arg(float, int precision = -1, int width = 0, char pad = ' ');
Format& arg(double, int precision = -1, int width = 0, char pad = ' ');
template<typename T>
Format& arg(T);

Expand Down
24 changes: 22 additions & 2 deletions lib/tlUI/FileBrowserButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,40 @@ namespace tl
p.fileInfo = fileInfo;
p.options = options;

// Icon.
switch (fileInfo.getType())
{
case file::Type::File:
setIcon("File");
break;
case file::Type::Directory:
setIcon("Directory");
break;
default: break;
}

// File name.
p.labels.push_back(fileInfo.getPath().get(-1, false));

// File sequence.
if (fileInfo.getPath().isSequence())
{
p.labels.push_back(fileInfo.getPath().getSequenceString());
}

// File extension.
switch (fileInfo.getType())
{
case file::Type::File:
setIcon("File");
p.labels.push_back(fileInfo.getPath().getExtension());
break;
case file::Type::Directory:
setIcon("Directory");
p.labels.push_back(std::string());
break;
default: break;
}

// File size.
std::string label;
const uint64_t size = fileInfo.getSize();
if (size < memory::megabyte)
Expand All @@ -106,6 +125,7 @@ namespace tl
}
p.labels.push_back(label);

// File last modification time.
const std::time_t time = fileInfo.getTime();
std::tm* localtime = std::localtime(&time);
char buffer[32];
Expand Down
5 changes: 5 additions & 0 deletions lib/tlUI/FileBrowserDirectoryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ namespace tl
_directoryUpdate();
}

void DirectoryWidget::reload()
{
_directoryUpdate();
}

void DirectoryWidget::setCallback(const std::function<void(const file::FileInfo&)>& value)
{
_p->callback = value;
Expand Down
2 changes: 2 additions & 0 deletions lib/tlUI/FileBrowserPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace tl

void setPath(const std::string&);

void reload();

void setCallback(const std::function<void(const file::FileInfo&)>&);

void setOptions(const FileBrowserOptions&);
Expand Down
27 changes: 26 additions & 1 deletion lib/tlUI/FileBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace tl

std::shared_ptr<Label> titleLabel;
std::shared_ptr<ToolButton> upButton;
std::shared_ptr<ToolButton> cwdButton;
std::shared_ptr<ToolButton> reloadButton;
std::shared_ptr<LineEdit> pathEdit;
std::shared_ptr<PathsWidget> pathsWidget;
std::shared_ptr<ScrollWidget> pathsScrollWidget;
Expand All @@ -47,6 +47,7 @@ namespace tl
std::shared_ptr<ComboBox> extensionsComboBox;
std::shared_ptr<ComboBox> sortComboBox;
std::shared_ptr<CheckBox> reverseSortCheckBox;
std::shared_ptr<CheckBox> sequenceCheckBox;
std::shared_ptr<PushButton> okButton;
std::shared_ptr<PushButton> cancelButton;
std::shared_ptr<Splitter> splitter;
Expand Down Expand Up @@ -92,6 +93,10 @@ namespace tl
p.upButton->setIcon("DirectoryUp");
p.upButton->setToolTip("Go up a directory");

p.reloadButton = ToolButton::create(context);
p.reloadButton->setIcon("Reload");
p.reloadButton->setToolTip("Reload the current directory");

p.pathEdit = LineEdit::create(context);
p.pathEdit->setHStretch(Stretch::Expanding);
p.pathEdit->setToolTip("The current directory");
Expand Down Expand Up @@ -122,6 +127,9 @@ namespace tl
p.reverseSortCheckBox = CheckBox::create("Reverse sort", context);
p.reverseSortCheckBox->setToolTip("Reverse the sort");

p.sequenceCheckBox = CheckBox::create("Sequence", context);
p.sequenceCheckBox->setToolTip("Show sequences of files");

p.okButton = PushButton::create(context);
p.okButton->setText("Ok");

Expand All @@ -139,6 +147,7 @@ namespace tl
auto hLayout = HorizontalLayout::create(context, vLayout);
hLayout->setSpacingRole(SizeRole::SpacingSmall);
p.upButton->setParent(hLayout);
p.reloadButton->setParent(hLayout);
p.pathEdit->setParent(hLayout);
p.splitter = Splitter::create(Orientation::Horizontal, context, vLayout);
p.splitter->setSplit(0.2);
Expand All @@ -156,6 +165,7 @@ namespace tl
label->setMarginRole(SizeRole::MarginInside);
p.sortComboBox->setParent(hLayout);
p.reverseSortCheckBox->setParent(hLayout);
p.sequenceCheckBox->setParent(hLayout);
auto spacer = Spacer::create(Orientation::Horizontal, context, hLayout);
spacer->setSpacingRole(SizeRole::None);
spacer->setHStretch(Stretch::Expanding);
Expand All @@ -171,6 +181,12 @@ namespace tl
_pathUpdate();
});

p.reloadButton->setClickedCallback(
[this]
{
_p->directoryWidget->reload();
});

p.pathEdit->setTextCallback(
[this](const std::string& value)
{
Expand Down Expand Up @@ -243,6 +259,14 @@ namespace tl
_p->directoryWidget->setOptions(options);
});

p.sequenceCheckBox->setCheckedCallback(
[this](bool value)
{
FileBrowserOptions options = _p->directoryWidget->getOptions();
options.list.sequence = value;
_p->directoryWidget->setOptions(options);
});

p.okButton->setClickedCallback(
[this]
{
Expand Down Expand Up @@ -318,6 +342,7 @@ namespace tl
}
p.sortComboBox->setCurrentIndex(static_cast<int>(value.list.sort));
p.reverseSortCheckBox->setChecked(value.list.reverseSort);
p.sequenceCheckBox->setChecked(value.list.sequence);
}

void FileBrowserWidget::setRecentFilesModel(const std::shared_ptr<RecentFilesModel>& value)
Expand Down
10 changes: 10 additions & 0 deletions tests/tlCoreTest/PathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ namespace tl
TLRENDER_ASSERT(i.extension == path.getExtension());
}
}
{
Path p("render.0001.exr");
const math::IntRange sequence(1, 100);
p.setSequence(sequence);
TLRENDER_ASSERT(sequence == p.getSequence());
TLRENDER_ASSERT(p.isSequence());
TLRENDER_ASSERT(p.sequence(Path("render.0101.exr")));
TLRENDER_ASSERT(!p.sequence(Path("render.101.exr")));
TLRENDER_ASSERT("0001-0100" == p.getSequenceString());
}
{
TLRENDER_ASSERT(Path("/").isAbsolute());
TLRENDER_ASSERT(Path("/tmp").isAbsolute());
Expand Down
8 changes: 8 additions & 0 deletions tests/tlCoreTest/StringFormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ namespace tl
const std::string s = Format("{0}{1}{2}").arg(1).arg(2).arg(3);
TLRENDER_ASSERT("123" == s);
}
{
const std::string s = Format("{0}").arg(1, 4);
TLRENDER_ASSERT(" 1" == s);
}
{
const std::string s = Format("{0}").arg(1, 4, '0');
TLRENDER_ASSERT("0001" == s);
}
{
const std::string s = Format("{0}").arg(int8_t(1));
TLRENDER_ASSERT("1" == s);
Expand Down
2 changes: 1 addition & 1 deletion tests/tltest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int main(int argc, char* argv[])
std::vector<std::shared_ptr<tests::ITest> > tests;
if (0)
{
tests.push_back(timeline_tests::EditTest::create(context));
tests.push_back(core_tests::PathTest::create(context));
}
else
{
Expand Down

0 comments on commit a6017b3

Please sign in to comment.