* The type ahead mechanism did not work correctly in list view mode: instead

of taking the contents of the columns, it would always use the file name
  instead.
* That uncovered another bug, though: WidgetText::Text() tried to cast everything
  to StringAttributeText, but GenericAttributeText is actually used for most
  columns.
* Therefore, GenericAttributeText is now a subclass from StringAttributeText.
* Extended the type ahead to take other columns into account, and made it also
  find strings in the middle (like a filter) as a second choice.
* Whitespace cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28180 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-10-16 20:18:20 +00:00
parent 1e2bc11e99
commit 49ad25b392
7 changed files with 225 additions and 115 deletions
+39 -22
View File
@@ -2166,7 +2166,7 @@ BPoseView::MessageReceived(BMessage *message)
BPose *pose = fSelectionList->FirstItem(); BPose *pose = fSelectionList->FirstItem();
if (pose) { if (pose) {
pose->EditFirstWidget(BPoint(0, pose->EditFirstWidget(BPoint(0,
fPoseList->IndexOf(pose) * fListElemHeight), this); fPoseList->IndexOf(pose) * fListElemHeight), this);
} }
break; break;
} }
@@ -6017,33 +6017,50 @@ BPoseView::FindNextMatch(int32 *matchingIndex, bool reverse)
BPose * BPose *
BPoseView::FindBestMatch(int32 *index) BPoseView::FindBestMatch(int32 *index)
{ {
char bestSoFar[B_FILE_NAME_LENGTH] = { 0 };
BPose *poseToSelect = NULL; BPose *poseToSelect = NULL;
float bestScore = -1;
BColumn *firstColumn = FirstColumn(); int32 count = fPoseList->CountItems();
size_t matchLength = strlen(sMatchString);
// loop through all poses to find match // loop through all poses to find match
int32 count = fPoseList->CountItems(); for (int32 j = 0; j < CountColumns(); j++) {
for (int32 i = 0; i < count; i++) { BColumn *column = ColumnAt(j);
BPose *pose = fPoseList->ItemAt(i);
const char * text;
if (ViewMode() == kListMode)
text = pose->TargetModel()->Name();
else {
ModelNodeLazyOpener modelOpener(pose->TargetModel());
BTextWidget *widget = pose->WidgetFor(firstColumn, this, modelOpener);
if (widget)
text = widget->Text();
else
text = pose->TargetModel()->Name();
}
if (strcasecmp(text, sMatchString) >= 0) for (int32 i = 0; i < count; i++) {
if (strcasecmp(text, bestSoFar) <= 0 || !bestSoFar[0]) { BPose *pose = fPoseList->ItemAt(i);
strcpy(bestSoFar, text); float score = -1;
if (ViewMode() == kListMode) {
ModelNodeLazyOpener modelOpener(pose->TargetModel());
BTextWidget *widget = pose->WidgetFor(column, this, modelOpener);
const char *text = NULL;
if (widget != NULL)
text = widget->Text(this);
if (text != NULL) {
score = ComputeTypeAheadScore(text, sMatchString,
matchLength);
}
} else {
score = ComputeTypeAheadScore(pose->TargetModel()->Name(),
sMatchString, matchLength);
}
if (score > bestScore) {
poseToSelect = pose; poseToSelect = pose;
bestScore = score;
*index = i; *index = i;
} }
if (score == kExactMatchScore)
break;
}
// TODO: we might want to change this to make it always work
// over all columns, but this would require some more changes
// to how Tracker represents data (for example we could filter
// the results out).
if (bestScore > 0 || ViewMode() != kListMode)
break;
} }
return poseToSelect; return poseToSelect;
@@ -6339,7 +6356,7 @@ BPoseView::WasClickInPath(const BPose *pose, int32 index, BPoint mouseLoc) const
if (widget->AttrHash() != AttrHashString(kAttrPath, B_STRING_TYPE)) if (widget->AttrHash() != AttrHashString(kAttrPath, B_STRING_TYPE))
return false; return false;
BEntry entry(widget->Text()); BEntry entry(widget->Text(this));
if (entry.InitCheck() != B_OK) if (entry.InitCheck() != B_OK)
return false; return false;
+4 -6
View File
@@ -92,15 +92,13 @@ BTextWidget::RecalculateText(const BPoseView *view)
const char * const char *
BTextWidget::Text() const BTextWidget::Text(const BPoseView *view) const
{ {
StringAttributeText *textAttribute = dynamic_cast<StringAttributeText *>(fText); StringAttributeText *textAttribute = dynamic_cast<StringAttributeText *>(fText);
if (textAttribute == NULL)
return NULL;
ASSERT(textAttribute); return textAttribute->ValueAsText(view);
if (!textAttribute)
return "";
return textAttribute->Value();
} }
+1 -1
View File
@@ -85,7 +85,7 @@ public:
bool IsActive() const; bool IsActive() const;
void SetActive(bool); void SetActive(bool);
const char *Text() const; const char *Text(const BPoseView *view) const;
// returns the untruncated version of the text // returns the untruncated version of the text
float TextWidth(const BPoseView *) const; float TextWidth(const BPoseView *) const;
float PreferredWidth(const BPoseView *) const; float PreferredWidth(const BPoseView *) const;
+73
View File
@@ -80,6 +80,8 @@ static const float kStubToStringSlotX = 5;
namespace BPrivate { namespace BPrivate {
const float kExactMatchScore = INFINITY;
const rgb_color kBlack = {0, 0, 0, 255}; const rgb_color kBlack = {0, 0, 0, 255};
const rgb_color kWhite = {255, 255, 255, 255}; const rgb_color kWhite = {255, 255, 255, 255};
@@ -1583,6 +1585,77 @@ BootedInSafeMode()
} }
float
ComputeTypeAheadScore(const char *text, const char *match, size_t matchLength,
bool wordMode)
{
float first = -1;
float second = -1;
float third = -1;
// highest score: exact match
float score = 0;
size_t pos = 0;
for (; pos < matchLength; pos++) {
if (text[pos] == '\0') {
score = 0;
break;
}
if (tolower(text[pos]) != tolower(match[pos]))
break;
score++;
}
if (pos == matchLength) {
// we don't need to look any further
return kExactMatchScore;
}
first = score;
// there was no exact match
// second best: all characters at word beginnings
if (wordMode) {
score = 0;
for (int32 j = 0, k = 0; match[j]; j++) {
while (text[k]
&& tolower(text[k]) != tolower(match[j])) {
k++;
}
if (text[k] == '\0') {
score = 0;
break;
}
bool wordStart = k == 0 || isspace(text[k - 1]);
if (wordStart)
score++;
if (j > 0) {
bool wordEnd = !text[k + 1] || isspace(text[k + 1]);
if (wordEnd)
score += 0.3;
if (match[j - 1] == text[k - 1])
score += 0.7;
}
score += 1.f / (k + 1);
k++;
}
second = score;
}
// acceptable last: exact match inside the string
score = 0;
const char* found = strstr(text + 1, match);
if (found != NULL)
score = 1.f / (found - text);
third = score;
return max_c(first, max_c(second, third));
}
void void
_ThrowOnError(status_t error, const char *DEBUG_ONLY(file), int32 DEBUG_ONLY(line)) _ThrowOnError(status_t error, const char *DEBUG_ONLY(file), int32 DEBUG_ONLY(line))
{ {
+6 -2
View File
@@ -587,7 +587,7 @@ inline void PrintDirToStream(const BDirectory *, const char * = 0) {}
thread_info info; \ thread_info info; \
get_thread_info(find_thread(NULL), &info); \ get_thread_info(find_thread(NULL), &info); \
PrintToLogFile("[t %Ld] \"%s\" (%s:%i) ", system_time(), \ PrintToLogFile("[t %Ld] \"%s\" (%s:%i) ", system_time(), \
info.name, __FILE__, __LINE__); \ info.name, __FILE__, __LINE__); \
PrintToLogFile _ARGS_; \ PrintToLogFile _ARGS_; \
PrintToLogFile("\n"); \ PrintToLogFile("\n"); \
fflush(logFile); \ fflush(logFile); \
@@ -616,6 +616,10 @@ inline int64 SwapInt64(int64 value) { return (int64)B_SWAP_INT64((uint64)value);
inline uint64 SwapUInt64(uint64 value) { return B_SWAP_INT64(value); } inline uint64 SwapUInt64(uint64 value) { return B_SWAP_INT64(value); }
extern const float kExactMatchScore;
float ComputeTypeAheadScore(const char *text, const char *match,
size_t matchLength, bool wordMode = false);
} // namespace BPrivate } // namespace BPrivate
#endif #endif // _UTILITIES_H
+38 -23
View File
@@ -520,15 +520,16 @@ WidgetAttributeText::SetDirty(bool value)
// #pragma mark - // #pragma mark -
StringAttributeText::StringAttributeText(const Model *model, const BColumn *column) StringAttributeText::StringAttributeText(const Model *model,
: WidgetAttributeText(model, column), const BColumn *column)
: WidgetAttributeText(model, column),
fValueDirty(true) fValueDirty(true)
{ {
} }
const char * const char *
StringAttributeText::Value() StringAttributeText::ValueAsText(const BPoseView * /*view*/)
{ {
if (fValueDirty) if (fValueDirty)
ReadValue(&fFullValueText); ReadValue(&fFullValueText);
@@ -573,7 +574,7 @@ StringAttributeText::PreferredWidth(const BPoseView *pose) const
int int
StringAttributeText::Compare(WidgetAttributeText &attr, BPoseView *) StringAttributeText::Compare(WidgetAttributeText &attr, BPoseView *view)
{ {
StringAttributeText *compareTo = StringAttributeText *compareTo =
dynamic_cast<StringAttributeText *>(&attr); dynamic_cast<StringAttributeText *>(&attr);
@@ -582,7 +583,7 @@ StringAttributeText::Compare(WidgetAttributeText &attr, BPoseView *)
if (fValueDirty) if (fValueDirty)
ReadValue(&fFullValueText); ReadValue(&fFullValueText);
return strcasecmp(fFullValueText.String(), compareTo->Value()); return strcasecmp(fFullValueText.String(), compareTo->ValueAsText(view));
} }
@@ -757,7 +758,7 @@ NameAttributeText::NameAttributeText(const Model *model, const BColumn *column)
int int
NameAttributeText::Compare(WidgetAttributeText &attr, BPoseView *) NameAttributeText::Compare(WidgetAttributeText &attr, BPoseView *view)
{ {
NameAttributeText *compareTo = dynamic_cast<NameAttributeText *>(&attr); NameAttributeText *compareTo = dynamic_cast<NameAttributeText *>(&attr);
@@ -769,7 +770,7 @@ NameAttributeText::Compare(WidgetAttributeText &attr, BPoseView *)
if (NameAttributeText::sSortFolderNamesFirst) if (NameAttributeText::sSortFolderNamesFirst)
return fModel->CompareFolderNamesFirst(attr.TargetModel()); return fModel->CompareFolderNamesFirst(attr.TargetModel());
return strcasecmp(fFullValueText.String(), compareTo->Value()); return strcasecmp(fFullValueText.String(), compareTo->ValueAsText(view));
} }
@@ -1122,8 +1123,7 @@ ModificationTimeAttributeText::ReadValue()
GenericAttributeText::GenericAttributeText(const Model *model, GenericAttributeText::GenericAttributeText(const Model *model,
const BColumn *column) const BColumn *column)
: WidgetAttributeText(model, column), : StringAttributeText(model, column)
fValueDirty(true)
{ {
} }
@@ -1133,7 +1133,7 @@ GenericAttributeText::CheckAttributeChanged()
{ {
GenericValueStruct tmpValue = fValue; GenericValueStruct tmpValue = fValue;
BString tmpString(fFullValueText); BString tmpString(fFullValueText);
ReadValue(); ReadValue(&fFullValueText);
// fDirty could already be true, in that case we mustn't set it to // fDirty could already be true, in that case we mustn't set it to
// false, even if the attribute text hasn't changed // false, even if the attribute text hasn't changed
@@ -1153,7 +1153,7 @@ GenericAttributeText::PreferredWidth(const BPoseView *pose) const
void void
GenericAttributeText::ReadValue() GenericAttributeText::ReadValue(BString *result)
{ {
BModelOpener opener(const_cast<Model *>(fModel)); BModelOpener opener(const_cast<Model *>(fModel));
@@ -1179,7 +1179,7 @@ GenericAttributeText::ReadValue()
// didn't read the whole attribute in or it wasn't to // didn't read the whole attribute in or it wasn't to
// begin with // begin with
fFullValueText = buffer; *result = buffer;
fValueIsDefined = true; fValueIsDefined = true;
} }
break; break;
@@ -1270,7 +1270,7 @@ void
GenericAttributeText::FitValue(BString *result, const BPoseView *view) GenericAttributeText::FitValue(BString *result, const BPoseView *view)
{ {
if (fValueDirty) if (fValueDirty)
ReadValue(); ReadValue(&fFullValueText);
fOldWidth = fColumn->Width(); fOldWidth = fColumn->Width();
@@ -1411,11 +1411,25 @@ GenericAttributeText::FitValue(BString *result, const BPoseView *view)
fDirty = false; fDirty = false;
return; return;
} }
fTruncatedWidth = TruncString(result, buffer, (ssize_t)strlen(buffer), view, fOldWidth); fTruncatedWidth = TruncString(result, buffer, (ssize_t)strlen(buffer), view,
fOldWidth);
fDirty = false; fDirty = false;
} }
const char*
GenericAttributeText::ValueAsText(const BPoseView *view)
{
// TODO: redesign this - this is to make sure the value is valid
bool oldDirty = fDirty;
BString result;
FitValue(&result, view);
fDirty = oldDirty;
return fFullValueText.String();
}
int int
GenericAttributeText::Compare(WidgetAttributeText &attr, BPoseView *) GenericAttributeText::Compare(WidgetAttributeText &attr, BPoseView *)
{ {
@@ -1424,14 +1438,15 @@ GenericAttributeText::Compare(WidgetAttributeText &attr, BPoseView *)
ASSERT(compareTo); ASSERT(compareTo);
if (fValueDirty) if (fValueDirty)
ReadValue(); ReadValue(&fFullValueText);
if (compareTo->fValueDirty) if (compareTo->fValueDirty)
compareTo->ReadValue(); compareTo->ReadValue(&compareTo->fFullValueText);
// Sort undefined values last, regardless of the other value: // Sort undefined values last, regardless of the other value:
if (fValueIsDefined == false || compareTo->fValueIsDefined == false) if (fValueIsDefined == false || compareTo->fValueIsDefined == false) {
return fValueIsDefined < compareTo->fValueIsDefined ? return fValueIsDefined < compareTo->fValueIsDefined ?
(fValueIsDefined == compareTo->fValueIsDefined ? 0 : -1) : 1; (fValueIsDefined == compareTo->fValueIsDefined ? 0 : -1) : 1;
}
switch (fColumn->AttrType()) { switch (fColumn->AttrType()) {
case B_STRING_TYPE: case B_STRING_TYPE:
@@ -1703,9 +1718,9 @@ GenericAttributeText::CommitEditedTextFlavor(BTextView *textView)
OpenWithRelationAttributeText::OpenWithRelationAttributeText(const Model *model, OpenWithRelationAttributeText::OpenWithRelationAttributeText(const Model *model,
const BColumn *column, const BPoseView *view) const BColumn *column, const BPoseView *view)
: ScalarAttributeText(model, column), : ScalarAttributeText(model, column),
fPoseView(view) fPoseView(view)
{ {
} }
@@ -1747,9 +1762,9 @@ OpenWithRelationAttributeText::FitValue(BString *result, const BPoseView *view)
VersionAttributeText::VersionAttributeText(const Model *model, VersionAttributeText::VersionAttributeText(const Model *model,
const BColumn *column, bool app) const BColumn *column, bool app)
: StringAttributeText(model, column), : StringAttributeText(model, column),
fAppVersion(app) fAppVersion(app)
{ {
} }
+11 -8
View File
@@ -140,9 +140,11 @@ WidgetAttributeText::TargetModel() const
class StringAttributeText : public WidgetAttributeText { class StringAttributeText : public WidgetAttributeText {
public: public:
StringAttributeText(const Model *, const BColumn *); StringAttributeText(const Model *, const BColumn *);
const char *Value();
virtual const char *ValueAsText(const BPoseView *view);
// returns the untrucated text that corresponds to the attribute // returns the untrucated text that corresponds to the attribute
// value // value
virtual bool CheckAttributeChanged(); virtual bool CheckAttributeChanged();
virtual float PreferredWidth(const BPoseView *) const; virtual float PreferredWidth(const BPoseView *) const;
@@ -156,6 +158,7 @@ class StringAttributeText : public WidgetAttributeText {
virtual void ReadValue(BString *result) = 0; virtual void ReadValue(BString *result) = 0;
virtual int Compare(WidgetAttributeText &, BPoseView *view); virtual int Compare(WidgetAttributeText &, BPoseView *view);
BString fFullValueText; BString fFullValueText;
bool fValueDirty; bool fValueDirty;
// used for lazy read, managed by ReadValue // used for lazy read, managed by ReadValue
@@ -200,32 +203,32 @@ union GenericValueStruct {
}; };
class GenericAttributeText : public WidgetAttributeText { class GenericAttributeText : public StringAttributeText {
// used for displaying mime extra attributes // used for displaying mime extra attributes
// supports different formats // supports different formats
public: public:
GenericAttributeText(const Model *, const BColumn *); GenericAttributeText(const Model *model, const BColumn *column);
virtual bool CheckAttributeChanged(); virtual bool CheckAttributeChanged();
virtual float PreferredWidth(const BPoseView *) const; virtual float PreferredWidth(const BPoseView *view) const;
virtual int Compare(WidgetAttributeText &, BPoseView *view); virtual int Compare(WidgetAttributeText &, BPoseView *view);
virtual void SetUpEditing(BTextView *); virtual void SetUpEditing(BTextView *);
virtual bool CommitEditedText(BTextView *); virtual bool CommitEditedText(BTextView *);
virtual const char *ValueAsText(const BPoseView *view);
private: private:
virtual bool CommitEditedTextFlavor(BTextView *); virtual bool CommitEditedTextFlavor(BTextView *);
virtual void FitValue(BString *result, const BPoseView *); virtual void FitValue(BString *result, const BPoseView *);
virtual void ReadValue(); virtual void ReadValue(BString *result);
// ToDo: // TODO:
// split this up into a scalar flavor and string flavor // split this up into a scalar flavor and string flavor
// to save memory // to save memory
BString fFullValueText;
GenericValueStruct fValue; GenericValueStruct fValue;
bool fValueDirty;
}; };